How to make a Java algorithm that can measure the running time of the Heap Sort sort sort algorithm?

Asked

Viewed 1,749 times

1

I started my algorithm this way so that I can measure its running time, but I’m not sure if it’s right, how to continue with an example:

package heapsort;

public class Heapsort {


    public static void main(String[] args) {

        int quantidade = 1000;
        int[] vetor = new int[quantidade];
        int tamanho;

        for(int i = 0; i < vetor.length; i++){
            vetor[i] = (int) (Math.random()*quantidade);
        }

        long tempoInicial = System.currentTimeMillis();


    }

}
  • Get the time immediately before running, get the time immediately after running. Subtract one from the other. Here’s the algorithm.

  • https://stackoverflow.com/questions/504103/how-do-i-write-a-correct-micro-benchmark-in-java

  • I have a project created to measure these things. I created to make this answer: https://answall.com/a/235636/64969

1 answer

0

You need to first store the initial currentTimeMillis() and then compare with the final.

long tempoInicial = System.currentTimeMillis();

// execução do método

System.out.println("O método foi executado em " + System.currentTimeMillis() - tempoInicial);

System.currentTimeMillis() is a function that returns the difference, measured in milliseconds, between the current time and midnight on January 1, 1970 UTC (coordinated universal time).

  • I believe System.currentTimeMillis() is more accurate

Browser other questions tagged

You are not signed in. Login or sign up in order to post.