Which is faster? Comparison or assignment?

前端 未结 12 1223
抹茶落季
抹茶落季 2020-12-05 04:16

I\'m doing a bit of coding, where I have to write this sort of code:

if( array[i]==false )
    array[i]=true;

I wonder if it should be re-w

12条回答
  •  旧时难觅i
    2020-12-05 04:44

    Might give this a try:

    if(!array[i])
        array[i]=true;
    

    But really the only way to know for sure is to profile, I'm sure pretty much any compiler would see the comparison to false as unnecessary and optimize it out.

提交回复
热议问题