How to effectively measure difference in a run-time

One of the exercises in TC++PL asks:

Write a function that either returns a value or that throws that value based on an argument. Measure the difference in run-time between the two ways.

Great pity he never explaines how to measure such things. I'm not sure if I'm suppose to write simple "time start, time end" counter, or are there more effective and practical ways?

Upvotes: 2

Views: 439

Answers (5)

ben
ben

Reputation: 2014

Consider using boost.timer, it is about as simple 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";

Upvotes: 3

MaxGuernseyIII
MaxGuernseyIII

Reputation:

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

Upvotes: 1

Kirill V. Lyadvinsky
Kirill V. Lyadvinsky

Reputation: 99555

He doesn't explain because that's part of exercise.

If seriously, I believe you should write simple "time start, time end" and big loop inside.

Upvotes: 1

yamspog
yamspog

Reputation: 18333

the simplest way would be to get the system time before and after your run. if that doesn't give enough resolution you can look into some of the higher-resolution system timers.

if you need more details you can use a commercial product like dot trace: http://www.jetbrains.com/profiler/

Upvotes: 0

James McNellis
James McNellis

Reputation: 355009

For each of the functions,

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

and compare the results. That's about as practical as performance measuring gets.

Upvotes: 5

Related Questions