0

I am testing a new Insertion Sort with Junit5 in Eclipse. I have already written three other tests, which all pass with no errors. What I want to test in test04 is if the time the Insertion Sort takes to sort an already sorted int[] is less than the time it takes to sort an exactly backwards int[].

Here is my code:

@Test
void test04() {
        // Let's test if sorting an already sorted array takes less time than sorting a backwards one

        int[] sorted = {1, 2, 3, 4, 5, 6, 7, 8, 9};
        int[] backwards = {9, 8, 7, 6, 5, 4, 3, 2, 1};

        long start = System.currentTimeMillis();
        InsertionSort.Sort(sorted);
        long sortedTime = System.currentTimeMillis() - start;

        start = System.currentTimeMillis();
        InsertionSort.Sort(backwards);
        long backwardsTime = System.currentTimeMillis() - start;

        System.out.println(sortedTime);
        System.out.println(backwardsTime);
        System.out.println(System.currentTimeMillis());

        assert(backwardsTime > sortedTime);
}

The problem is that my timing mechanism(using System.currentTimeMillis()) is not working at all! Here is the console output:

0
0
1587044842939

sortedTime and backwardsTime are equal to 0! So test04 is failing because 0 is not greater than 0. Interestingly enough, when I print out System.currentTimeMillis(), it gives me a good normal looking number. I'm wondering what is up? Is this a bug? Am I doing something wrong?

Any help is welcome!

Captain Jack Sparrow
  • 971
  • 1
  • 11
  • 28
  • 2
    And what do you think the value is if it takes **less** then a millisecond? Also the actual value and accuracy depends on your system. – M. Deinum Apr 16 '20 at 14:03
  • How long do you think it should take to sort a 9 element array on anything remotely modern? – nitind Apr 16 '20 at 14:04
  • @M.Deinum I'm not sure. I doubt that it takes less than a millisecond though. – Captain Jack Sparrow Apr 16 '20 at 14:04
  • A unit test that is testing performance like this seems very strange to me. I would just like to suggest another option: have your sort method return the number of comparisons or the number of swaps that were performed. (Or, provide some way to instrument the code to get those values.) Then, you can compare the work done by the methods instead of the time taken. – David Conrad Apr 16 '20 at 18:53
  • @DavidConrad That's a good idea, makes sense. – Captain Jack Sparrow Apr 16 '20 at 20:54

2 Answers2

2

Both input sizes are very small, thus chances are high that it took less than 1 ms. You can use System.nanoTime to measure the time in nanoseconds.

akuzminykh
  • 4,522
  • 4
  • 15
  • 36
  • 2
    You are welcome. :-) *FYI:* Benchmarking at nanosecond precision like that can give you wrong results because all kind of stuff affects the runtime. At such high precision this will blur your results. You can check this by running the tests multiple times. Chances are high that the values will vary much. You should check out [this](https://stackoverflow.com/questions/504103/how-do-i-write-a-correct-micro-benchmark-in-java) post. – akuzminykh Apr 16 '20 at 14:18
2

This is likely taking less than a millisecond to complete. Use System.nanoTime instead to get nanoseconds.

        long start = System.nanoTime();

        InsertionSort.Sort(sorted);

        long sortedTime = System.nano() - start;
Jason
  • 5,154
  • 2
  • 12
  • 22