1

I have a curiosity. I would like to know if it is more efficient this code

int a(0);
for(int i=0;i!=10;++i){
    a=0;
    for(int j=0;j!=10;++j){
        // perform some operations on a
    }
} 

or this code

for(int i=0;i!=10;++i){
    int a(0);
    for(int j=0;j!=10;++j){
        // perform some operations on a
    }
} 

or it is exactly the some in term of performance. I understand that the answer may depend on the compiler and its inner optimization technique but I am curios to understand if there is any difference.

Thank you, Marco.

2
  • 2
    Depends on the implementation. If the implementers have done their job there should be no difference and any deteriorations are going to be taken care of the optimizer.
    – 101010
    Jun 4, 2014 at 15:22
  • +1 welcome on board ;)
    – Wolf
    Jun 4, 2014 at 15:38

1 Answer 1

3

In any modern C++ compiler there's no difference what-so-ever.

1
  • 2
    +1 for answering the C++ question (in the former question, there is very much to read)
    – Wolf
    Jun 4, 2014 at 15:36

Not the answer you're looking for? Browse other questions tagged or ask your own question.