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");
No comments:
Post a Comment