Original Post
🔒 Locked
local variable vs loop local variable
Advertisement
What are your criteria for ''better''?
Anyway...
alternatives:
1) Design your software to make use of containers that can be used in the standard library algorithms. You''ll then use for_each and the many similar functions which perform the loops for you.
2) Move the loop into a function of its own. Then the difference between the two examples you give will be unimportant.
What machine code does your compiler produce?
Anyway...
alternatives:
1) Design your software to make use of containers that can be used in the standard library algorithms. You''ll then use for_each and the many similar functions which perform the loops for you.
2) Move the loop into a function of its own. Then the difference between the two examples you give will be unimportant.
What machine code does your compiler produce?
quote:
Original post by petewood
What are your criteria for ''better''?
Indeed. But the usual C++ way of doing it is to have a "local loop variable", like you put it. Any decent compiler should eliminate the speed difference between the two. It is also in line with the thinking that a variable (or object) shouldn''t be declared before you are going to use it.
Cédric
In the original example, there isn''t, but what I had in mind is
i.e.: the reuse of the iteration variable. A very dumb compiler could push and pop i twice in the second example... It''s pointless, I know, but that''s what sprung to mind when I answered.
Cédric
int i;
for(i=0;i<10;++i){...}
for(i=0;i<30;++i){...}
vs
for(int i=0;i<10;++i){...}
for(int i=0;i<30;++i){...}
i.e.: the reuse of the iteration variable. A very dumb compiler could push and pop i twice in the second example... It''s pointless, I know, but that''s what sprung to mind when I answered.
Cédric
Topic Locked
This topic has been locked by a moderator. New replies are not allowed.
Sign in to reply to this topic.
Advertisement
