Declare an object inside or outside a loop?

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

Is there any performance penalty for the following code snippet?

for (int i=0; i         


        
16条回答
  •  攒了一身酷
    2020-12-07 18:00

    As someone who maintains more code than writes code.

    Version 1 is much preferred - keeping scope as local as possible is essential for understanding. Its also easier to refactor this sort of code.

    As discussed above - I doubt this would make any difference in efficiency. In fact I would argue that if the scope is more local a compiler may be able to do more with it!

提交回复
热议问题