Saturday, October 17, 2009

How to Compute Program Execution Time in Java

I don't think you need to compute your program execution time everytime you execute your program but sometimes when you need to compare performance then it becomes necessary to compute the execution time.When you need to compare two programming styles then you may consider calculating the time required by each program program to complete the task ,lesser the time the more fast the program is.The fact is since the processors are too fast now days and due to even faster RAM's the million of loops complete within split of a second.So there is need to calculate speed till the precision of 1000th of a second.Here in order to compute the time we will use the currentTimeMillis() method of System class which returns current time of your computer system.There is a very simple logic which is implemented to create this utility.

In the following example we will compute the program execution time of main() method.As we enter the main() method we will compute the system time and after implementation of programming logic or business logic we will again compute the system time.Remember the system time is returned as long in milliseconds.So now you have two values first when we started our main() method and second just before exiting the main.We assume here is that the program exits immediately after getting second value.

Source code and Example:-

// Method to be used System.currentTimeMillis()
//This method returns long values.

class timecalc
{
public static void main(String a[]){
long time = System.currentTimeMillis();
System.out.println(time);
for(int i=0;i<5000000;i++)
{
for(int j=0;j<1000;j++);
}
long end = System.currentTimeMillis();
System.out.println(end-time);
}
}

Output :-
3782
Remember the output may vary according to your computers speed.speed is affected by various factors like RAM,processor speed,running programs,cache etc.

Here in above example I have taken such long loop because just to calculate the time difference and to just give you a demo how things goes.When you compute for several hundreds of values then it is possible that it might return 0.Everytime your run your program and you are expected to get a different value.

Suggested Reading :-

Compute any day and date in calendar
Get date and time in your specified format
How to execute system programs from your programs?
Most common scoping errors commited by Novice programmers

0 comments:

About This Blog

This Blog is all about Java and programming.This blog is written by Vaibhav Pandey .He is a Computer Science Graduate .You can contact the author at javatute@gmail.com for any suggestion or Query.You can also contact the author for advertising on this blog.All the material presented here is the property of author and its reproduction in any form is strictly prohibited.

Disclaimer:-Download links provided here are not of author in any means.These are found over the Internet.
Page copy protected against web site content infringement by Copyscape

  © Blogger templates The Professional Template by Ourblogtemplates.com 2008

Back to TOP