Because I am constantly busy working on something, I have never had time to actually put everything in words and pictures. But, since you got here, then you must have already seen some part of my work - and this is the way I’m talking.I'm 23, born in Romania, student at UPG Romania in software development field. I started from 0, mostly with basic stuff, and I’m evolving every day to an expert. I'm focused on freelancing projects, from small websites, to really heavy stuff. I know that I look and act differently from most developers, but this is why you will love to work with me!

Friday, September 23, 2011

How to measure the time of a process


To measure the time of a process just call the System.nanoTime method when process starts and again when process ends. The time of the process will be the diference betwen the two calls.

package time;

public class Time {

public static void main(String[] args) {

long start_time, end_time, elapsedTime;

start_time = System.nanoTime();

// ... the code being measured ...

end_time = System.nanoTime();
elapsedTime = end_time - start_time;

System.out.println("Start: " + start_time);
System.out.println("End : " + end_time);
...

The System.nanoTime method returns values in nanoseconds, to convert nanoseconds to miliseconds or seconds you just need to make a few divisions:

System.out.println("The process took approximately:\n "
+elapsedTime+" nano seconds\n "
+(elapsedTime/1000000.0)+" miliseconds\n "
+(elapsedTime/1000000000.0)+" seconds");
This tip can be found at: http://download.oracle.com/javase/7/docs/api/

No comments: