Calculate Time of Sorting an Array in Java



In this article, we will learn to measure the time taken to sort an array in Java. We will look at two methods to calculate sorting time: using the Date class and the System.nanoTime() method.

First, we will use the Date class to track the time before and after the sorting operation, allowing us to calculate the duration in milliseconds. Then, we'll utilize System.nanoTime() to capture the time with greater precision, measuring in nanoseconds and converting the result to milliseconds.

Different approaches

Below are the different approaches to measure the time taken to sort an array using Java?

Using Date class 

The following are the steps to calculate the time of sorting an array using Date class ?

  • First, we will import the Arrays class for sorting and the Date class to track time from java.util package.
  • Create an array of integers and fill it with a sequence of numbers.
  • We will capture the current time before sorting begins.
  • The Arrays.sort() method to sort the array.
  • Record the end time and capture the current time after sorting is complete.
  • At the end, we will calculate the difference between the end and start times, and display the sorting time in milliseconds.

Example

Below is the Java program to calculate the time of sorting an array using Date class ?

import java.util.Arrays;
import java.util.Date;
public class Demo {
   public static void main(String[] args) {
      int[] arr = new int[1000];
      for (int i = 0; i < arr.length; i++) {
         arr[i] = (int) (i + 20);
      }
      Date past = new Date();
      Arrays.sort(arr);
      Date future = new Date();
      System.out.println("Time (milliseconds) = " + (future.getTime() - past.getTime()));
   }
}

Output

Time (milliseconds) = 2

Using System.nanoTime()

Below is the Java program to calculate the time of sorting an array using System.nanoTime() ?

  • First, we will import the Arrays class from java.util package.
  • We initialize an integer array of size 1000 and fill it with values starting from 20 and instead of using Date, we capture the current time in nanoseconds.
  • We call Arrays.sort() to sort the array.
  • We again use System.nanoTime() to capture the time after sorting.
  • The duration of the sorting operation is found by subtracting the start time from the end time.
  • Finally, we print the sorting time in both nanoseconds and milliseconds (by dividing the duration by 1,000,000).

Example

Below is the Java program to calculate the time of sorting an array using System.nanoTime() ?

import java.util.Arrays;
public class Demo {
    public static void main(String[] args) {
        int[] arr = new int[1000];
        for (int i = 0; i < arr.length; i++) {
            arr[i] = (int) (i + 20);
        }
        long startTime = System.nanoTime();
        Arrays.sort(arr); // Sort the array
        long endTime = System.nanoTime(); 
        long duration = endTime - startTime; 
        System.out.println("Time (nanoseconds) = " + duration);
        System.out.println("Time (milliseconds) = " + (duration / 1_000_000));
    }
}

Output

Time (nanoseconds) = 187525
Time (milliseconds) = 0
Updated on: 2024-10-25T11:21:27+05:30

747 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements