Avoiding if statement inside a for loop?

后端 未结 4 2013
南旧
南旧 2020-12-04 06:54

I have a class called Writer that has a function writeVector like so:

void Drawer::writeVector(vector vec, bool index=true         


        
4条回答
  •  不知归路
    2020-12-04 07:23

    In the function, I'm doing the if (index) check on every round of my for-loop, even though the result is always the same. This is against "worrying about the performance".

    If this is indeed the case, the branch predictor will have no problem in predicting the (constant) result. As such, this will only cause a mild overhead for mispredictions in the first few iterations. It's nothing to worry about in terms of performance

    In this case I advocate for keeping the test inside the loop for clarity.

提交回复
热议问题