2

I'm currently trying to measure the real elapsed cpu time for a part of my code in java. I read that to get the CPU time you have to use the

cpuTime = ManagementFactory.getThreadMXBean().getCurrentThreadCpuTime();

but in fact when I try to measure it in that piece of code, an error is thrown when the elapsed time is equal to zero..... (which it appears to me to be impossible with a nano-sec precision). The while loop can be big or can also very small (aout 10 instuctions min).

long startTime = ManagementFactory.getThreadMXBean()
                        .getCurrentThreadCpuTime();
// reset with seed solution for each neighborRule and
// each pivoting rule
t.setCurrentBestSolution(seedSolution);
while (t.chooseNextImprovingNeighborSolution(pivotingRule,
                        neighborRule));                 
long endTime = ManagementFactory.getThreadMXBean()
                    .getCurrentThreadCpuTime();
if (endTime - startTime == 0) {
    System.err.println(pivotingRule + ":" + neighborRule);
    System.err.println("END TIME:" + endTime);
    System.err.println("START TIME:" + startTime);
                }

Any idea ? Am I not using properly the CPUThread part ? Should I use an external java benchmarker ?

Thanks in advance

BodXav
  • 23
  • 1
  • 3
  • 1
    You can't accurately measure execution time more precisely than the ns. You have to build a loop or another way to make you code longer if you want to benchmark it. – Denys Séguret Apr 04 '13 at 16:13

2 Answers2

1

Should I use an external java benchmarker ?

Yes please do. Jprofiler measures the real elapsed time among other useful metrics.

Deepak Bala
  • 11,095
  • 2
  • 38
  • 49
  • JProfiler mostly use for web-based software! I think nobody don't use it for `console` applications. `System` and `Runtime` class may help better. What do you think? – boomz Apr 04 '13 at 16:17
  • What gave you that idea ? I used it to profile plain java applications all the time. You just need to hook up the agent correctly on VM startup. – Deepak Bala Apr 04 '13 at 16:20
  • I'll try to see if JProfiler make it work. Having 0 isn't so annoying but What's the goal of having nanosecond precision if you cannot measure it precisely. Thanks for your answers – BodXav Apr 04 '13 at 17:09
  • The trouble is that nanoTime() does not offer nano level accuracy. Just nano level precision. The update resolution of nanoTime() affects your results. A native DLL / SO should be able to work around that by using the highest precision native clock with a better update resolution. Let us know how it panned out when you're done. – Deepak Bala Apr 04 '13 at 17:29
-2

The methods of System and Runtime class will help you. Take a look on:

http://docs.oracle.com/javase/1.5.0/docs/api/java/lang/System.html http://docs.oracle.com/javase/1.5.0/docs/api/java/lang/Runtime.html

For example: You can use: System.currentTimeMillis(); which returns the time in millisecond, before and after your program! (at the beginning or end of main method!)

boomz
  • 657
  • 5
  • 21