java performance

Java programmers often write the code to test the performance of the certain classes , normally we wrote code like this

long start = System.currentTimeMillis();
long end = System.currentTimeMillis();
long duration1 = end - start;

But there is a better way to do this . you can use ThreadMXBean.getCurrentThreadCpuTime()
function in jdk.

you can use

ThreadMXBean mx = ManagementFactory.getThreadMXBean();
long start = mx.getCurrentThreadCpuTime();
long end = mx.getCurrentThreadCpuTime();
long duration1 = end - start;

java documentation here

0 opinions: