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

后端 未结 8 2102
离开以前
离开以前 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 14:54

    At the machine level there is no such thing as bool

    Very few instruction set architectures define any sort of boolean operand type, although there are often instructions that trigger an action on non-zero values. To the CPU, usually, everything is one of the scalar types or a string of them.

    A given compiler and a given ABI will need to choose specific sizes for int and bool and when, like in your case, these are different sizes they may generate slightly different code, and at some levels of optimization one may be slightly faster.

    Why is bool one byte on many systems?

    It's safer to choose a char type for bool because someone might make a really large array of them.

    Update: by "safer", I mean: for the compiler and library implementors. I'm not saying people need to reimplement the system type.

提交回复
热议问题