tags:

views:

248

answers:

4

I'm using a built-in benchmarking module for some quick and dirty tests. It gives me:

  • CPU time
  • system CPU time (actually I never get any result for this with the code I'm running)
  • the sum of the user and system CPU times (always the same as the CPU time in my case)
  • the elapsed real time

I didn't even know I needed all that information.

I just want to compare two pieces of code and see which one takes longer. I know that one piece of code probably does more garbage collection than the other but I'm not sure how much of an impact it's going to have.

Any ideas which metric I should be looking at?

And, most importantly, could someone explain why the "elapsed real time" is always longer than the CPU time - what causes the lag between the two?

+3  A: 

Multitasking operating system, stalls while waiting for I/O, and other moments when you code is not actively working.

Chris Dolan
Correct, concise, and undiluted by trivial examples. I wish your answer wasn't so under-appreciated.
Devin Jeanpierre
Hey, @Devon, I won't take that as a personal swipe at my answer :-) I prefer to leave examples, however trivial, if they can add to understanding. BTW, did you know that your bio page says you're going to take an internship in 20010 - do you think you'll live that long?
paxdiablo
Appreciation is in the mind of the question poster. I don't feel slighted at all. :-)
Chris Dolan
+5  A: 

There are many things going on in your system other than running your Ruby code. Elapsed time is the total real time taken and should not be used for benchmarking. You want the system and user CPU times since those are the times that your process actually had the CPU.

An example, if your process:

  • used the CPU for one second running your code; then
  • used the CPU for one second running OS kernel code; then
  • was swapped out for seven seconds while another process ran; then
  • used the CPU for one more second running your code,

you would have seen:

  • ten seconds elapsed time,
  • two seconds user time,
  • one second system time,
  • three seconds total CPU time.

The three seconds is what you need to worry about, since the ten depends entirely upon the vagaries of the process scheduling.

paxdiablo
Terrific explanation. Thanks so much. That really cleared it up for me.
Jeff
There is another effect to beware of: Time your program spends e.g., waiting for IO does not count towards CPU time.
derobert
+1  A: 

It may also be the case that the CPU time when your code is executing is not counted.

The extreme example is a real-time system where the timer triggers some activity which is always shorter than a timer tick. Then the CPU time for that activity may never be counted (depending on how the OS does the accounting).

starblue
+1  A: 

You don't want to totally discount clock-on-the-wall time. Time used to wait w/o another thread ready to utilize CPU cycles may make one piece of code less desirable than another. One set of code may take some more CPU time, but, employ multi-threading to dominate over the other code in the real world. Depends on requirements and specifics. My point is ... use all metrics available to you to make your decision.

Also, as a good practice, if you want to compare two pieces of code you should be running as few extraneous processes as possible.

jeffD