C++ - the fastest integer type?
c++, performance
Solution
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.
Problem
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.