Reputation: 24675
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
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
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
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
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
Reputation: 355009
For each of the functions,
and compare the results. That's about as practical as performance measuring gets.
Upvotes: 5