Savvion Help

Do you need help on Savvion?
E-mail us on for any information, help, how-to or questions and we will contact you and reply to your question as soon as possible.

Monday, August 8, 2011

Calculate time taken to execute the code in Java

Have you ever thought of how much processing time is taken to execute certain code or a method? It is good approach to check the execution time taken if you are calling an external api's or a web service for which you do not have the control.

Below is the code by which you can do it in a simple way in Java.

System.out.println("Calling service method...");
long t1 = System.currentTimeMillis();
//Below will be your complex code which you expect will take time and is critical
Object[] entity = service_port.getInfoByID(1103);
System.out.println("Time taken for Info service = " + ((System.currentTimeMillis() - t1) + " milli seconds");

The above code will print the time taken to execute the code in milliseconds. This will allow you to check if the complex code is taking much of the execution time and hence forth you can optimize your code and api's used to reduce the execution time. It is a very good idea to check the execution time if your application is going to run on real time which can affect the business.

No comments: