for loop variable optimization in C/C++ -
which faster in following 2 code snippets? , why?
declared loop index variable outside for
statement:
size_t = 0; (i = 0; < 10; i++) { }
and
declared loop index variable within for
statement:
for (size_t = 0; < 10; i++) { }
neither, equivalent , yield same machine code.
(the compiler remove redundant initialization of i
twice first example.)
where variable declared has little performance , memory use.
for (size_t = 0; < 10; i++)
considered readable.
for (i = 0; < 10; i++)
has advantage can use i
variable after loop done - makes sense when number of iterations variable.
Comments
Post a Comment