问题
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. However char/signed char was slower. Difference: 1.07x.
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.
回答1:
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. 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
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.
回答2:
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.
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. 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...).
回答3:
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. Or slower, depending upon other factors.
回答4:
The following are typedefs of fundamental integral types or extended integral types.
check fast mod. you can find out for other types(char) fast mod as well.
library is :: cstdint
uint_fast8_t :: my suggestion
http://www.cplusplus.com/reference/cstdint/
??you may need to know about the architecture of machine you are using!!
回答5:
As it was said int
in most cases represent the machine word. 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.
While if you use char
it is 4 times smaller (on x86 systems) than int
and also 4 times smaller than processor register. So before it will be put to RAM it should be truncated. As a result more time is used.
Furthermore, processor which has 32bits register can't perform operations with 8bits number. If char is add to char they both are put to register. So the each register will have 8bits of char value and 24bits of trash. Two 32bits values will be added and then the result will be back truncated to 8bits. The reason why char
and short
works the same time is the fact that the same number of additional operations is used. While for int
additional operations are not done.
I would like to add that for processor int
and unsigned int
is completely the same as it treats them in the same way. For some compilers int
and long int
also may be the same.
So the fastest integer type is the type which length is the same as machine word. If you use types with smaller size than machine word the program will work slower.
来源:https://stackoverflow.com/questions/10015715/c-the-fastest-integer-type