简体   繁体   English

C++ - 最快的 integer 类型?

[英]C++ - the fastest integer type?

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).主要组件是缓冲区(原始整数数组)和索引器(整数 - 用于访问缓冲区中的元素)。

The fastest types for the buffer seem to be unsigned char, and both signed and unsigned versions of short, int, long.缓冲区最快的类型似乎是 unsigned char,以及 short、int、long 的有符号和无符号版本。 However char/signed char was slower.但是 char/signed char 速度较慢。 Difference: 1.07x.差异:1.07 倍。

For the indexer there was no difference between signed and unsigned types.对于索引器,有符号和无符号类型之间没有区别。 However int and long were 1.21x faster than char and short.然而,int 和 long 比 char 和 short 快 1.21 倍。

Is there a type that should be used by default when considering performance and not memory consumption?在考虑性能而不是 memory 消耗时,是否应该默认使用一种类型?

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.通常最大的胜利来自缓存。

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.如果您的数据值足够小,可以容纳 8 位,那么与使用 int 并浪费 3 个字节/值相比,您可以在 CPU 缓存中容纳更多数据。 If you are processing a block of data you get a huge speed advantage for cache hits.如果您正在处理一个数据块,您将在高速缓存命中方面获得巨大的速度优势。

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 寄存器(即不要尝试在 8 位 CPU 上使用long long ),它就会有相同的速度

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.您需要多次运行该算法以允许缓存,您需要观察 CPU 上还有什么在运行,甚至还有哪些其他硬件可能正在中断。 Speed differences of 10% might be considered noise unless you are very careful.除非您非常小心,否则 10% 的速度差异可能会被视为噪音。

It depends heavily on the underlying architecture.它在很大程度上取决于底层架构。 Usually fastest data types are those that are word-wide.通常最快的数据类型是那些字宽的。 In my experience with IA32 (x86-32), smaller/bigger than word data types incur in penalties, sometimes even more than one memory read for one single data.根据我使用 IA32 (x86-32) 的经验,小于/大于 word 数据类型会导致惩罚,有时甚至超过一次 memory 读取一个数据。

Once on the CPU registers, usually data type length doesn't matter (if the whole data fits in one register, that is) but what operations you accomplish with them.一旦进入 CPU 寄存器,通常数据类型长度并不重要(如果整个数据适合一个寄存器,也就是说),而是您用它们完成的操作。 Of course floating point operations are the most costly;当然浮点运算是最昂贵的; the fastest being adding, subtracting (which is also comparing), bit-wise (shift and the like), and logical operations (and, or...).最快的是加法、减法(也是比较)、按位(移位等)和逻辑运算(和、或...)。

There are no promises about which type is faster or slower.没有关于哪种类型更快或更慢的承诺。 int is supposed to represent the natural word length of the machine, whatever that might mean, so it might go faster. int应该代表机器的自然字长,不管它是什么意思,所以它可能 go 更快。 Or slower, depending upon other factors.或更慢,取决于其他因素。

The following are typedefs of fundamental integral types or extended integral types.以下是基本整数类型或扩展整数类型的 typedef。

check fast mod.检查快速模式。 you can find out for other types(char) fast mod as well.您也可以找到其他类型(字符)的快速模组。

library is:: cstdint图书馆是:: cstdint

uint_fast8_t:: my suggestion uint_fast8_t:: 我的建议

http://www.cplusplus.com/reference/cstdint/ http://www.cplusplus.com/reference/cstdint/

??you may need to know about the architecture of machine you are using!! ??您可能需要了解您正在使用的机器的体系结构!!

As it was said int in most cases represent the machine word.正如所说, int在大多数情况下代表机器字。 So int will have the same length as processor register has, so no additional actions won't be done to put int to register and than back to RAM.因此int将具有与处理器寄存器相同的长度,因此不会执行任何其他操作来将int放入寄存器而不是返回 RAM。

While if you use char it is 4 times smaller (on x86 systems) than int and also 4 times smaller than processor register.如果您使用char ,它比int小 4 倍(在 x86 系统上),也比处理器寄存器小 4 倍。 So before it will be put to RAM it should be truncated.所以在将它放入 RAM 之前,它应该被截断。 As a result more time is used.结果使用了更多的时间。

Furthermore, processor which has 32bits register can't perform operations with 8bits number.此外,具有 32 位寄存器的处理器无法执行 8 位数字的操作。 If char is add to char they both are put to register.如果将 char 添加到 char,则它们都将被注册。 So the each register will have 8bits of char value and 24bits of trash.所以每个寄存器将有 8 位的 char 值和 24 位的垃圾。 Two 32bits values will be added and then the result will be back truncated to 8bits.将添加两个 32 位值,然后结果将被截断为 8 位。 The reason why char and short works the same time is the fact that the same number of additional operations is used. charshort同时工作的原因是使用了相同数量的附加操作。 While for int additional operations are not done.而对于int额外的操作没有完成。

I would like to add that for processor int and unsigned int is completely the same as it treats them in the same way.我想补充一点,处理器intunsigned int完全相同,因为它以相同的方式对待它们。 For some compilers int and long int also may be the same.对于某些编译器, intlong int也可能相同。

So the fastest integer type is the type which length is the same as machine word.所以最快的 integer 类型是长度与机器字相同的类型。 If you use types with smaller size than machine word the program will work slower.如果您使用比机器字更小的类型,程序将运行得更慢。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM