C++ - the fastest integer type?

前端 未结 5 1101
不思量自难忘°
不思量自难忘° 2021-02-09 14:00

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

5条回答
  •  长发绾君心
    2021-02-09 14:20

    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.

提交回复
热议问题