Which is faster : if (bool) or if(int)?

后端 未结 8 2127
离开以前
离开以前 2020-12-07 14:48

Which value is better to use? Boolean true or Integer 1?

The above topic made me do some experiments with bool and in

8条回答
  •  佛祖请我去吃肉
    2020-12-07 15:15

    Makes sense to me. Your compiler apparently defines a bool as an 8-bit value, and your system ABI requires it to "promote" small (< 32-bit) integer arguments to 32-bit when pushing them onto the call stack. So to compare a bool, the compiler generates code to isolate the least significant byte of the 32-bit argument that g receives, and compares it with cmpb. In the first example, the int argument uses the full 32 bits that were pushed onto the stack, so it simply compares against the whole thing with cmpl.

提交回复
热议问题