Is it ok to use 64bit integers in a 32bit application?

前端 未结 2 2395
无人及你
无人及你 2021-02-20 18:18

I notice in C and C++, we can use int64_t, or simply a long long.

If I compile 32bit code using these types, will I suffer any perform

2条回答
  •  一整个雨季
    2021-02-20 18:44

    If I compile 32bit code using these types, will I suffer any performance issues on 64bit and/or 32bit machines?

    64 bit integers on 32 bit architectures require two registers to store the value*. 32 bit values require only one register. This matters because:

    • You may run out of registers sooner, requiring registers to be spilled to memory. This takes more time.
    • Loading and storing 2 registers typically takes two instructions, not one.
    • Doing addition or subtraction on operands in two registers takes two or more instructions versus just one for operands in just one register.

    Bottom line is that if 32 bit performance is important, don't use 64 bit integers unless you need them.s

    * This is true for x86, ARM, and PowerPC processors, which covers most of the processors people program for these days. It is probably true of most other processors as well.

提交回复
热议问题