optimization - Is it legal for a C++ optimizer to reorder calls to clock()? -
optimization - Is it legal for a C++ optimizer to reorder calls to clock()? -
the c++ programming language 4th edition, page 225 reads: a compiler may reorder code improve performance long result identical of simple order of execution. compilers, e.g. visual c++ in release mode, reorder code:
#include <time.h> ... auto t0 = clock(); auto r = verylongcomputation(); auto t1 = clock(); std::cout << r << " time: " << t1-t0 << endl;
into form:
auto t0 = clock(); auto t1 = clock(); auto r = verylongcomputation(); std::cout << r << " time: " << t1-t0 << endl;
which guarantees different result original code (zero vs. greater 0 time reported). see my other question detailed example. behavior compliant c++ standard?
the compiler cannot exchange 2 clock
calls. t1
must set after t0
. both calls observable side effects. compiler may reorder between observable effects, , on observable side effect, long observations consistent possible observations of abstract machine.
since c++ abstract machine not formally restricted finite speeds, execute verylongcomputation()
in 0 time. execution time not defined observable effect. real implementations may match that.
mind you, lot of reply depends on c++ standard not imposing restrictions on compilers.
c++ optimization clock
Comments
Post a Comment