Reputation: 6415
What is the easiest way to know whether a certain chunk of code will be optimized in a certain way, in Visual Studio?
For example, the line marked below is very likely to be optimized out.
int main(){
int a=3; //####
std::cout<<"1234"<<std::endl;
}
This can be checked moderately easily by setting a break-point, it will be grey-out.
What if the code is more complex?
Below are just examples.
It would be ideal if I can know it without compiling.
Example 1
int f1(){
return 3;
}
int main(){
int a=f1(); //#### likely to be in-line ?
std::cout<<a<<std::endl;
}
How can I be sure about that f() will be in-line?
Example 2
void f2(bool a){
if(a) std::cout<<"T"<<std::endl;
else std::cout<<"F"<<std::endl;
}
int main(){
f2(true);
f2(false);
}
How can I know whether f2() will be optimized by split into 2 functions like this:-
void f2_true(){ std::cout<<"T"<<std::endl; }
void f2_false(){ std::cout<<"F"<<std::endl; }
int main(){
f2_true();
f2_false();
}
Upvotes: 2
Views: 142
Reputation: 738
As mentioned multiple times in the comments, predicting the optimization is not useful. However, you can let your compiler give you some information of what optimizations he actually did. For example you can use the options /Qvec-report:2 with the warnings at least at level 4 (/W4) to get information about the vectorization of code. The Microsoft webpage has detailed information on how to interpret the output of this option. I found this option very useful to achieve parallelization of critical code fragments, which resulted in performance gains of up to a factor of 4.
I suppose there are other flags to let the compiler be verbose about other optimizations.
Upvotes: 3