3

When evaluating my algorithms against one another in regards to time taken should I use CPU or User time in Java? I used the information here: How to get CPU, system, and user time for benchmarking, to get these values...

Here we go:

"User time" is the time spent running your application's own code.
"System time" is the time spent running OS code on behalf of your application (such as for I/O).

We often refer to "CPU time" as well:

"CPU time" is user time plus system time. It's the total time spent using a CPU for your application.

Which is most appropriate for CS experiments?

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
José
  • 1,525
  • 3
  • 14
  • 12

2 Answers2

3

Surely it depends on your algorithm.

If your algorithm performs no I/O itself, user time is the most accurate measure of how good it is.

But if your algorithm does for example disk-based sorting, the time that includes I/O is more appropriate.

Note though that the Java VM itself runs code of its own too, most notably GC, which from the OS point of view also counts as user time, so if you want to be really thorough, you can try to minimise the impact of GC by logging GC running times and subtracting them from the user time as measured by the OS. (On the other hand, you can argue that if the two algorithms are run under the same conditions and one triggers a lot more GC runs than the other, that's a fault in the algorithm itself.)

Update: Sorry, I've just read the page you linked and of course if you measure the user time of the thread, the above doesn't apply, only if you look at user time of the entire (heavyweight) process.

biziclop
  • 48,926
  • 12
  • 77
  • 104
2

Often elapse time is the only thing which matters. If you can write a program which uses multiple cores (which increases the CPU time) but reduces the amount of time the user is waiting, this is preferable. Elapse time includes IO, user and system time.

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130