There is a great question about the "as-if" rule in general, but I wonder if there are any exceptions when it comes to measuring time.
Consider this (taken from here slightly modified):
using std::chrono;
auto begin = steady_clock::now();
auto result = some_lengthy_calculation(some_params);
auto end = std::chrono::steady_clock::now();
std::cout << "Time diff = " << duration_cast<microseconds>(end - begin).count() <<std::endl;
std::cout << "Result = " << result;
The compiler is allowed to apply any optimization that results in the same result
. The point here is that the "as-if" rule does not apply to the measured time directly. Of course the measured time is not something that should be constant when optimizations are applied.
So my question is: How is it possible that I can reliably measure time with the above code when, according to the "as-if" rule, the compiler is allowed to rearrange it to one of the following?
auto temp = some_lengthy_calculation(some_params); // clever "optimization", precompute some stuff
auto begin = steady_clock::now();
auto result = temp; // yay, I can use it here to pretend to be faster
auto end = steady_clock::now();
std::cout << "Time diff = " << duration_cast<microseconds>(end - begin).count() <<std::endl;
std::cout << "Result = " << result;
or even "more optimized":
std::cout << "Time diff = " << 42 <<std::endl;
std::cout << "Result = " << some_lengthy_calculation(some_params);
I assume no sane compiler would do that, but what exactly prevents a compiler from doing such "optimization"?
TL;DR...
- One can observe the runtime difference between optimized and non-optimized code
- If compiler optimizations are allowed to effect measured times, what prevents the compiler from not creating any code for the timing at all?