Pages

Saturday, March 24, 2018

Program Execution Time


This JAVA program will help you to find total time taken by FOR loop of the program. You can extend this JAVA program to find total execution time of the program.

Program will display Total time taken in milliseconds. 

Program Execution Time

class TimeTaken 
{
    public static void main(String[] args) 
    {
        long startTime = System.currentTimeMillis();
        long sum = 0;
        for (int i = 0; i < 10000000; i++) 
        {
            sum = sum + i;
        }
        long endTime = System.currentTimeMillis();
        long timetaken = endTime - startTime;

        System.out.println("FOR loop execution time = " + timetaken + " milliseconds.");
    }
}

Output of program

FOR loop execution time = 14 milliseconds.

No comments:

Post a Comment