Monday, August 8, 2011
Calculate time taken to execute the code in Java
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.