This example program will describe you the way that how one can calculate or get the execution time of a method or program in java. This can be done by subtracting the starting time of the program or method by the ending time of the method. In our example java program we are using System.currentTimeMillis() method for getting current time in milliseconds.
We have created a method callMethod() and before calling this method we have stored the current time in a variable startTime and after the method is done with its working we have stored the current time into another variable endTime. Now by subtracting the startTime with endTime we can calculate the execution time of method.
Here is the full example code of GetExecutionTime.java as follows:
Value of counter is 1
Value of counter is 2
Value of counter is 3
Value of counter is 4
Value of counter is 5
Value of counter is 6
Value of counter is 7
Value of counter is 8
Value of counter is 9
Value of counter is 10
Total elapsed time in execution of method callMethod() is :16
If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.
Ask your questions, our development team will try to give answers to your questions.