Is if(var == true) faster than if(var != false)?

后端 未结 10 1054
礼貌的吻别
礼貌的吻别 2020-12-18 18:25

Pretty simple question. I know it would probably be a tiny optimization, but eventually you\'ll use enough if statements for it to matter.

EDIT: Thank you to those o

10条回答
  •  余生分开走
    2020-12-18 18:49

    The other answers are all good, I just wanted to add:

    This is not a meaningful question, because it assumes a 1:1 relation between the notation and the resulting IL or native code.

    There isn't. And that's true even in C++, and even in C. You have to go all the way down to native code to have such a question make sense.

    Edited to add:

    The developers of the first Fortran compiler (ca. 1957) were surprised one day when reviewing its output. It was emitting code that was not obviously correct (though it was); in essence, it was making optimization decisions that were not obviously correct (though they were).

    The moral of this story: compilers have been smarter than people for over 50 years. Don't try to outsmart them unless you're prepared to examine their output and/or do extensive performance testing.

提交回复
热议问题