Creating an object in the loop

后端 未结 7 549
夕颜
夕颜 2021-01-14 00:56
 std::vector C(4);
 for(int i = 0; i < 1000;++i)
  for(int j = 0; j < 2000; ++j)
  {   
   C[0] = 1.0;
   C[1] = 1.0;
   C[2] = 1.0;
   C[3] = 1.         


        
7条回答
  •  别那么骄傲
    2021-01-14 01:15

    First thing you should do is making sure that the design is OK, this means:

    • Is the code easy to understand
    • Does the code prevent itself against bugs
    • Is the code easily extensible

    I think that in this case it would indeed imply that it's better to define the variable in the loop.

    Only if you have real performance problems, you may optimize your code (if the compiler didn't already do this for you), e.g. by putting the variable declaration outside the loop.

提交回复
热议问题