I want to measure the speed of a function within a loop. But why my way of doing it always print ‘0’ instead of high-res timing with 9 digits decimal precision (i.e. in nano/micro seconds)?
What’s the correct way to do it?
#include <iomanip> #include <iostream> #include <time.h> int main() { for (int i = 0; i <100; i++) { std::clock_t startTime = std::clock(); // a very fast function in the middle cout << 'Time: ' << setprecision(9) << (clock() - startTime + 0.00)/CLOCKS_PER_SEC << endl; } return 0; }
Move your time calculation functions outside
for () { .. }statement then devide total execution time by the number of operations in your testing loop.Note: std::clock() lacks sufficient precision for profiling. Reference.