Saturday, 3 March 2018

performance - C++ Efficency of variable declaration




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.


Answer



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


No comments:

Post a Comment

casting - Why wasn't Tobey Maguire in The Amazing Spider-Man? - Movies & TV

In the Spider-Man franchise, Tobey Maguire is an outstanding performer as a Spider-Man and also reprised his role in the sequels Spider-Man...