C++ - the fastest integer type?

前端 未结 5 1029
不思量自难忘°
不思量自难忘° 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:25

    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.

提交回复
热议问题