Execution time is defined as the time taken from the start to the end of a certain task. When the end time is subtracted from the start time the result is obtained as the execution time. This plays a vital role in knowing how much time a certain process takes to execute.
In this particular write-up, we will execute the System.currentTimeMillis() in Java.
How to Measure Execution Time with System.currentTimeMillis() in Java
To measure the execution time in Java there are several methods used like the currentTimeMills method which returns the time in milliseconds which we will use here. The nanoTime() method is responsible for returning the time in nanoseconds. The now() method is responsible for returning the current time and the duration in Java.
Syntax
The syntax for the time in milliseconds is depicted as:
System.currentTimeMillis()
Now, let us implement the above syntax in the code below:
Example 1: Calculate Execution Time in Milliseconds
This coding example illustrates how to compute execution time in milliseconds using the currentTimeMillis():
public class javaprogram {
public static void main(String[] args) {//Use the long() data type because of milliseconds
long startingtime = System.currentTimeMillis();
output();
long endingtime = System.currentTimeMillis();
long timerunning = endingtime - startingtime;//print the running time in milliseconds
System.out.println("Running Time: " + timerunning+ "ms");
}
public static void output() {//print the overall results in milliseconds
System.out.println("Printing the results in Milliseconds");
}
}
In the code above:
- The long data type is used.
- System.currentTimeMillis() is a built-in method that is used to get results in milliseconds.
- The ending time has been subtracted from the starting time to get the total elapsed time.
- A statement has been passed to measure the time elapsed for that particular statement.
Output

The output above gives the running time in milliseconds.
Example 2: Using Recursion
This coding example illustrates how to compute execution time in milliseconds using the recursion
public class javaprogram {//The factorial for implementing recursion.
public int factprogram(int y){//Declare the conditions
if(y!=0)
return y*factprogram(y-1);
else
return 1;
}
public static void main(String[] args) {
javaprogram fact=new javaprogram ();
long startingtime = System.currentTimeMillis();//Inserting a large value, to calculate the time
fact.factprogram(1025);
long endingtime = System.currentTimeMillis();
long timerunning = endingtime - startingtime;
System.out.println("Running Time: " + timerunning+ "ms");
}
}
The code is the same as the previous one except for the difference:
- Recursion has been implemented using a factorial program.
- A large value has been passed to calculate the time elapsed in milliseconds.
Output

The output above shows the running time in milliseconds.
Conclusion
To measure the execution time in milliseconds there is a built-in function in Java known as System.currentTimeMillis(). This function prints the execution time in milliseconds for the statement or integer value passed. In this write-up, an example of recursion using a factorial program has been implemented to develop clarity about the execution time.