Reputation: 592
I am doing a project where I need to implement bubblesort, shellsort and quicksort algorithms in an array of 999 random doubles.
I need to measure the time it takes every algorithm to run.
I am using System.nanoTime() to measure this execution time for each algorithm. I have 3 buttons, one for each algorithm. Upon clicking a button, the timer starts, the function is called, the end timer is then called and the duration is calculated by having endtime-starttime. The duration is then printed on a label on top of the button.
public double[] randomArray = SortAlg.getArray();
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
long startTimeBubble = System.nanoTime();
SortAlg.bubble(randomArray);
long endTimeBubble = System.nanoTime();
long durationBubble = (endTimeBubble - startTimeBubble) / 1000000;
bubbleTiempo.setText("bubble took " + durationBubble + " ms");
}
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
long startTimeShellSort = System.nanoTime();
SortAlg.shellSort(randomArray);
long endTimeShellSort = System.nanoTime();
long durationShellSort = (endTimeShellSort - startTimeShellSort) / 1000000;
shellSortTime.setText("ShellSort took " + durationShellSort + " ms");
}
private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {
long startTimeQuickSort = System.nanoTime();
SortAlg.quickSort(randomArray, 0, randomArray.length - 1);
long endTimeQuickSort = System.nanoTime();
long durationQuickSort = (endTimeQuickSort - startTimeQuickSort) / 1000000;
quickSortTime.setText("Quicksort took " + durationQuickSort + " ms");
}
General clarifications: 1-getArray() generates a random array of 999 real (double) numbers between 0 and 2000.
2-None of the algorithm methods return an array, so this randomArray is NOT being saved as sorted, therefore, every time the button is clicked, the random array that is passed into the function is unsorted.
3-Every button has a unique label to display the results.
4-What you're seeing there is from the GUI class I created with Netbeans GUI editor, the buttons portion. Main simply creates a new GUI frame and sets it as visible, and the SortAlg contains the logic for the algorithms, which I already tested to be working.
But I have 2 issues here: If I click the button more than 1 time, the time decreases further for every click, until it's eventually 0, which makes no sense.
Also, I initially had a calculation error and I was dividing the time taken by 100000 rather than 1000000, but oddly enough, this was never giving me any errors. When I divide it by 1000000 though (which should be the correct operation for converting ns to ms) I sometimes get 0 MS execution time, which again makes no sense.
Lastly, and I know that this might be difficult to determine, but I was under the impression that quickSort had to be the fastest algorithm? But their execution times greatly vary within builds. Sometimes each one is 5 ms, other times its 30, other times its 68. I am concerned because it seems that, the first clicked is always the fastest one and the other ones end up being slower. I am not confident about these results at all.
Upvotes: 0
Views: 105
Reputation: 117675
When I divide it by 1000000 though (which should be the correct operation for converting ns to ms) I sometimes get 0 MS execution time, which again makes no sense.
Because you are using integer division:
long durationBubble = (endTimeBubble - startTimeBubble) / 1000000;
^^^^^^^
Just replace it with double
to include the fraction:
double durationBubble = (endTimeBubble - startTimeBubble) / 1_000_000.0;
See:
Upvotes: 3