views:

99

answers:

5

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?

+5  A: 

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.

James McNellis
Thanks, that's what I also was thinking about.
There is nothing we can do
A: 

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/

yamspog
+1  A: 

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.

Kirill V. Lyadvinsky
Thanks for your answer. Sometimes (although I consider this book to be excellent) I'm bit annoyed by his lack of any even tinest hints to excersises. I think of an excersise as a my try to prove that I've learned what I've been studing (from last chapter i.e.) but when he didn't even bother to say: aha and listen the way of how to measure this things you have to find out for yourself, then I'm not sure, if I've missed something in this or maybe in previous chapters and I have to first check them or just did what I've did now. Thanks anyway.
There is nothing we can do
A: 

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.

I didn't give you the drive-by, and I agree with your point, Max, but I don't see how it's relevant to the question.
Mike Dunlavey
@Mike Dunlavey: Seriously? Is the question not how to measure the performance of something?
It's not clear that any way of measuring this is *not* wall-clock time, but even if it's CPU-only time (excluding I/O and time spent in competing processes) it's still going to be meaningful in this case. The reason I agree with your point in general is my experience with performance tuning says often people miss performance problems because their profiler excludes I/O, and because they think accurate absolute CPU time is more important than approximate percent wall-clock time.http://stackoverflow.com/questions/1777556/alternatives-to-gprof/1779343#1779343
Mike Dunlavey
Ah. That's where the disconnect is. CPU time is not wall-clock time. For instance: If there are two CPUs and two independent threads, CPU time could possibly be 2x wall-clock time. Likewise, if the thread that is being measured is blocking a lot (for I/O or whatever), then the CPU time will be less than wall-clock time. My position is that none of those measurements are relevant. Only wall-clock time.
I violently agree :-) My only point was that, *in this case*, what's being looked for is a ratio of times between the two cases (returning a value vs. throwing it) and since there's no I/O, it won't matter much *how* it is measured.
Mike Dunlavey
It's not just I/O. You have no way of knowing what other blockages there might be that get trimmed out as "not CPU time" by an exception. If it's big enough that it spans time-slices, the time is going to be time it took to start + time spent waiting for the next time-slice + time to complete.
Mike Dunlavey
Okay. The last word is this then: You can't know that until you actually measure it. You can believe it but not know it. Technically, I guess, you can't know it after you measure it either, but you get close enough for government work.
+3  A: 

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";
ben