Java time to run code in ns
I am trying to find the time taken by the Java 开发者_如何学JAVAcode to run, I am creating some huge object and want to track the time to the precision of nano seconds, if I use regular time stamp it says the code compiled in 0 seconds. Thanks
long start = System.nanoTime();
System.out.println("Hello World!");
long end = System.nanoTime();
System.out.println(end - start);
The docs
edit
it says the code compiled in 0 seconds
So, do you want compilation time or running time?
You need to take a look at
System.nanoTime()
The docs do say that nanosecond accuracy cannot be guaranteed, but it will give you more accurate results than 0seconds.
http://download.oracle.com/javase/1.5.0/docs/api/java/lang/System.html#nanoTime()
精彩评论