0

Here's my question: I want to time how long the program takes to sort an array. I try to print the result afterward, but it prints out a 0. Here's my main method:

   public static void main(String[] args) {
       
       for ( int b = 0; b < array1.length; b++) {
          int n = (int)(Integer.MAX_VALUE * Math.random());
          array1[b] = n;
          array2[b] = n;
        }//end of for loop
       
       long startTime = System.currentTimeMillis();
       insertionSort(array1);
       long runTime = System.currentTimeMillis() - startTime;
       System.out.println("The runtime of the sorting is "+ runTime);

   
    }//end of main

And here's my output:

The runtime of the sorting is 0

Thanks in advance!

  • And you missed the most important tag. The language you are using. – epascarello Jun 21 '22 at 15:27
  • 1
    https://stackoverflow.com/questions/180158/how-do-i-time-a-methods-execution-in-java – epascarello Jun 21 '22 at 15:29
  • Your precision is 1 millisecond. Are you absolutely certain that sorting your array does in fact take longer than 1 ms? The usual method is to perform some task repeatedly before dividing the elapsed time by this multiple. You don't time adding two numbers. You time adding 2 numbers a million or 10,000,000 times and then you can work out the single iteration time. – enhzflep Jun 21 '22 at 15:34

0 Answers0