Home Java Tutorials Use currentTimeMillis Method Example for Java Timer – Calculate Process Execution Time

Software Configurations

Follow Me

Use currentTimeMillis Method Example for Java Timer – Calculate Process Execution Time

When we creating software in any language we need to optimize the code. That mean the process execution time should be less. This java tutorial shows how to calculate process execution time in java by using currentTimeMillis method. This method return the current time (System time).

 

ProcessTime.java

 

//copyrighted by topsourcecode.com
import javax.swing.JOptionPane;

public class ProcessTime 
{     
    public static void main(String []a)
    {
        final long startTime = System.currentTimeMillis();
        final long endTime;
        try 
        {
            Process();
        }
        finally 
        {
            endTime = System.currentTimeMillis();  
        }
        final long duration = (endTime - startTime);
        JOptionPane one = new JOptionPane();
        JOptionPane.showMessageDialog(one,"Time Taken: "+duration + " miliSeconds");   
    }

   //You can add any method to this to check process time
    private static void Process() 
    {
        for(int i=0;i<1000000000;i++)
        {
        }
    } 
}

 

Result

Share this post