question about scoping and performance

I don't really understand caching as well as I would like, but as I understand it, it's good practice to scope variables to only exist within the range that they will be used:

1
2
3
4
5
6
7
8
9
10
11
for (blah blah) {
  // do stuff
  {
    vector<int>::iterator it_int = intvector.begin();
    vector<unsigned>::iterator it_unsigned = longvector.begin();
    for (size_t x = 0; x != 10; x++, it_int++, it_unsigned++) {
      // do something
    }
  }
  // do stuff
}


This is the way I've been writing my code where I can't declare the variables inside the for construct. Is this the best practice? I know that declarations should be in the inner most scope, but should I bother creating scope to limit the life of variables.

Also, will those iterators now be destroyed and recreated at every iteration of the outermost loop, or will the compiler just redefine them at every iteration?
Also, will those iterators now be destroyed and recreated

"Creating" and "destroying" objects are purely abstract concepts. In the end, often enough, it's just an allotment of storage (one or several registers or a location on the stack), which is a logistical thing and does not actually result in any code being generated.
So your chances that a line like vector<int>::iterator it_int = intvector.begin(); results in just a single move instruction are rather good.

I know that declarations should be in the inner most scope, but should I bother creating scope to limit the life of variables.

The way you did it is common practice, although I'm not even sure what kind of alternative you had in mind.
Thanks Athar, so basically I'll keep plowing ahead! FYI, the alternative I had in mind was simply removing all extra scoping, which would have the effect of polluting higher scopes with unecessary iterators (note the commented out curly brackets):

1
2
3
4
5
6
7
8
9
10
11
12
13
for (blah blah) {
  // do stuff

  // {
    vector<int>::iterator it_int = intvector.begin();
    vector<unsigned>::iterator it_unsigned = longvector.begin();
    for (size_t x = 0; x != 10; x++, it_int++, it_unsigned++) {
      // do something
    }
  // }

  // do stuff
}
Wait, I completely missed the extra block. Artificially limiting the scope of those iterators makes no sense - it doesn't even change anything, unless there's more code after the inner loop.
There's extra, unrelated code wherever I wrote "do stuff"; so before and after the scoping brackets. Could you clarify?
closed account (zb0S216C)
Personally, I frown upon what you're doing within the loop. Many pushes and pops going on in there which do affect performance. Think of it this way:

1) The outermost loop is set to loop 5 times.
2) Inside the outermost scope, a new, anonymous, temporary scope is made, which means a new stack frame on the stack.
3) 2 std::vector::iterators are declared; a further 2 pushes on the stack.
4) A for loop is declared - another push (stack frame) - and x is declared - a further push.
5) Yet another stack frame is pushed onto the stack (innermost).
6) The innermost loop ends followed by the pop of the innermost stack frame.
7) The temporary scope now becomes the innermost scope. The 2 iterators are popped, followed by the pop of the temporary stack frame.
8) The outermost loop starts again. Back to step 2 you go.

Overall, after 1 cycle, 6 pushes and 4 pops are made. I wouldn't follow that convention, even if you paid me.

Wazzak
I would like to see some proofs that it does affect performance at all. I think I would prefer like you do it ausairman.

EDIT: I tested putting the declaration of it_int and it_unsigned outside the loop and the compiler generated the same code.
Last edited on
Topic archived. No new replies allowed.