-
Notifications
You must be signed in to change notification settings - Fork 1.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
benchmark::DoNotOptimize not behaving as expected at -O1, -O2 and -O3 #242
Comments
benchmark::DoNotOptimize
not behaving as expected at -O1, -O2 and -O3benchmark::DoNotOptimize
not behaving as expected at -O1, -O2 and -O3
benchmark::DoNotOptimize
not behaving as expected at -O1, -O2 and -O3
DoNotOptimize(...) can only help prevent the optimization of the result, and not the intermediate expressions. |
I'll look into this more over the weekend. |
So if I understand you correctly, then if |
Essentially yes that is correct, except the optimizations can happen even without constexpr.
As you noted in your example the compiler optimized In my experience it's important to give the benchmark different inputs on every iteration to prevent this kind of optimization from taking place. |
I checked in a slightly improved version of Thanks for the report. It's greatly appreciated. |
For the following code:
Compiled on Ubuntu 16.04, with clang version
and
CPPFLAGS:= -std=c++14 -O3
, no call tostd::pow
is made; to wit, when the assembly is spat out withclang++ $(CPPFLAGS) -S -masm=intel test.cpp
, and the output examined withI do not see any call to
pow
, however, when compiling with -O0, I do see a call topow
.Even at
-O0
, I do not see the call topow
within the while loop, but rather it is called only once.This is a workaround:
Then the call to
pow
is observed in the loop. However, this seems a heroic response to a user error rather than a necessity.Is there a better way?
The text was updated successfully, but these errors were encountered: