Question

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

Was it helpful?

Solution 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!)

OTHER TIPS

Should I use an external java benchmarker ?

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top