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