5

Im wondering if this code:

int main(){
    int p;
    for(int i = 0; i < 10; i++){
      p = ...;
    }
    return 0
}

is exactly the same as that one

int main(){
    for(int i = 0; i < 10; i++){
      int p = ...;
    }
    return 0
}

in term of efficiency ? I mean, the p variable will be recreated 10 times in the second example ?

4 Answers 4

9
  • It's is the same in terms of efficiency.
  • It's not the same in terms of readability. The second is better in this aspect, isn't it?

It's a semantic difference which the code keeps hidden because it's not making a difference for int, but it makes a difference to the human reader. Do you want to carry the value of whatever calculation you do in ... outside of the loop? You don't, so you should write code that reflects your intention.

A human reader will need to seek the function and look for other uses of p to confirm himself that what you did was just premature "optimization" and didn't have any deeper purpose.

Assuming it makes a difference for the type you use, you can help the human reader by commenting your code

/* p is only used inside the for-loop, to keep it from reallocating */
std::vector<int> p;
p.reserve(10);

for(int i = 0; i < 10; i++){
  p.clear();
  /* ... */
}
Sign up to request clarification or add additional context in comments.

2 Comments

Well, it is equal in terms of efficiency only if your compiler doesn't zero stack variable.
I don't worry myself much with efficiency at first, but readability is a premium. Once it's working, if it's too slow, then it's time to profile it and optimize what can be.
4

In this case, it's the same. Use the smallest scope possible for the most readable code.

If int were a class with a significant constructor and destructor, then the first (declaring it outside the loop) can be a significant savings - but inside you usually need to recreate the state anyway... so oftentimes it ends up being no savings at all.

One instance where it might make a difference is containers. A string or vector uses internal storage that gets grown to fit the size of the data it is storing. You may not want to reconstruct this container each time through the loop, instead, just clear its contents and it may not need as many reallocations inside the loop. This can (in some cases) result in a significant performance improvement.

The bottom-line is write it clearly, and if profiling shows it matters, move it out :)

Comments

1

They are equal in terms of efficiency - you should trust your compiler to get rid of the immeasurably small difference. The second is better design.

Edit: This isn't necessarily true for custom types, especially those that deal with memory. If you were writing a loop for any T, I'd sure use the first form just in case. But if you know that it's an inbuilt type, like int, pointer, char, float, bool, etc. I'd go for the second.

Comments

0

In second example the p is visible only inside of the for loop. you cannot use it further in your code. In terms of efficiency they are equal.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.