Declare an object inside or outside a loop?

前端 未结 16 813
广开言路
广开言路 2020-12-07 17:15

Is there any performance penalty for the following code snippet?

for (int i=0; i         


        
16条回答
  •  死守一世寂寞
    2020-12-07 17:49

    The first makes far more sense. It keeps the variable in the scope that it is used in. and prevents values assigned in one iteration being used in a later iteration, this is more defensive.

    The former is sometimes said to be more efficient but any reasonable compiler should be able to optimise it to be exactly the same as the latter.

提交回复
热议问题