Saturday, March 3, 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

plot explanation - Why did Peaches' mom hang on the tree? - Movies & TV

In the middle of the movie Ice Age: Continental Drift Peaches' mom asked Peaches to go to sleep. Then, she hung on the tree. This parti...