C++ - 最快的整数类型? [英] C++ - the fastest integer type?

查看:46
本文介绍了C++ - 最快的整数类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在对算法进行基准测试,没有必要了解细节.主要组件是缓冲区(整数的原始数组)和索引器(整数 - 用于访问缓冲区中的元素).

I've being benchmarking an algorithm, it's not necessary to know the details. The main components are a buffer(raw array of integers) and an indexer (integer - used to access the elements in buffer).

缓冲区的最快类型似乎是无符号字符,以及有符号和无符号版本的short、int、long.然而,char/signed char 较慢.差异:1.07x.

The fastest types for the buffer seem to be unsigned char, and both signed and unsigned versions of short, int, long. However char/signed char was slower. Difference: 1.07x.

对于索引器,有符号和无符号类型没有区别.但是 int 和 long 比 char 和 short 快 1.21 倍.

For the indexer there was no difference between signed and unsigned types. However int and long were 1.21x faster than char and short.

在考虑性能而不是内存消耗时,是否应该默认使用一种类型?

Is there a type that should be used by default when considering performance and not memory consumption?

注意:对缓冲区元素和索引器使用的操作是赋值、递增、递减和比较.

NOTE: The operations used on the elements of the buffer and the indexer were assignment, increment, decrement and comparison.

推荐答案

通常最大的优势来自缓存.

Generally the biggest win comes from cacheing.

如果您的数据值足够小以适合 8 位,那么与使用整数并浪费 3 个字节/值相比,您可以在 CPU 缓存中容纳更多数据.如果您正在处理一个数据块,您将在缓存命中方面获得巨大的速度优势.

If your data values are small enough that they fit in 8 bits then you can fit more of the data in the CPU cache than if you used ints and wasted 3 bytes/value. If you are processing a block of data you get a huge speed advantage for cache hits.

索引的类型不太重要,只要它适合 CPU 寄存器(即不要尝试在 8 位 CPU 上使用 long long)它就会有相同的速度

The type of the index is less important, as long as it fits in a CPU register (ie don't try using a long long on an 8bit CPU) it will have the same speed

还值得一提的是,测量速度很棘手.您需要多次运行算法以允许缓存,您需要观察 CPU 上正在运行的其他内容,甚至其他硬件可能会中断.除非您非常小心,否则 10% 的速度差异可能会被视为噪音.

edit: it's also worth mentioning that measuring speed is tricky. You need to run the algorithm several times to allow for caching, you need to watch what else is running on the CPU and even what other hardware might be interrupting. Speed differences of 10% might be considered noise unless you are very careful.

这篇关于C++ - 最快的整数类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆