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.

Marco Agnese
  • 349
  • 4
  • 15
  • 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 04 '14 at 15:22
  • +1 welcome on board ;) – Wolf Jun 04 '14 at 15:38

1 Answers1

3

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

Paul Evans
  • 27,315
  • 3
  • 37
  • 54
  • 2
    +1 for answering the C++ question (in the former question, there is very much to read) – Wolf Jun 04 '14 at 15:36