How to efficiently measure runtime differences

One exercise in TC ++ PL asks:

Write a function that either returns a value or outputs that value based on an argument. Measure the difference in execution time between the two.

It's a shame that he never explains how to measure such things. I'm not sure if I can write a simple counter of time, end time, or more efficient and practical?

+2


source to share


5 answers


For each of the functions

  • Get start time
  • Call the function a million times (or more ... a million isn't that much, really)
  • Get the end time and subtract the start time from it


and compare the results. This is about as practical as measuring performance.

+5


source


Consider using boost.timer, it's about as easy as it gets.



#include <iostream>
#include <boost/timer.hpp>

boost::timer timer;
for (int i = 0; i < 100000; ++i) {
  // ... whatever you want to measure
}
std::cout << timer.elapsed() << " seconds.\n";

      

+3


source


He does not explain because this is part of the exercise.

Seriously though, I think you should write a simple "start time", "end time" and a big loop inside.

+1


source


The only dimension that ever matters is wall clock time. Don't let anyone else trick you into believing in something else. Read Lean Thinking if you don't believe it yourself.

+1


source


The easiest way is to get the system time before and after startup. if that doesn't give you enough resolution, you can look into some higher resolution system timers.

If you need more information, you can use a commercial product like point tracing: http://www.jetbrains.com/profiler/

0


source







All Articles