1

I have created a sample class which contains two integer array; one sorted and other unsorted. And I am printing contents of the array. Time taken to print sorted array is twice the time time required to print unsorted array.

Refer the code:

public class Demo {

public static void main(String[] args) {

    int[] array1 = {1,2,3,4,5,6,7,8,9,10};
    int[] array2 = {10,3,4,2,6,7,8,1,5,9};

    long start = System.nanoTime();

    for(int a:array1){
        System.out.println(a);
    }

    System.out.println("Time required by sorted array\t"+(System.nanoTime() - start) / 1000000000.0);

    start = System.nanoTime();
    for(int a:array2){
        System.out.println(a);
    }

    System.out.println("Time required by unsorted array\t"+(System.nanoTime() - start) / 1000000000.0);
}

}

Output:

1
2
3
4
5
6
7
8
9
10
Time required by sorted array   3.89505E-4
10
3
4
2
6
7
8
1
5
9
Time required by unsorted array 1.37727E-4

Why is there difference in time while displaying the numbers. Also I read somewhere that sorted array process faster, here the case is different.

amaurya123
  • 159
  • 10
  • 3
    The first time section also includes the time needed to load all the System.out classes. I bet if you reverse the order of your program, printing the unsorted values first, you'll see the opposite behavior. Profiling Java apps is notoriously difficult -- there are many resources online that talk about the pitfalls, and their solutions. – yshavit Mar 07 '17 at 13:35
  • 3
    To expand on yshavit's comment here's your example with the order changed: http://ideone.com/I3e0Bf - suddenly "unsorted" is slower – UnholySheep Mar 07 '17 at 13:36
  • 2
    Also one run it is not enough. You'd need several tens or hundreds runs to make such statement – bichito Mar 07 '17 at 13:36
  • @yshavit That makes sense.. – amaurya123 Mar 07 '17 at 13:41
  • @UnholySheep I noticed that as well.. i guess it is safe to say that processing unsorted array takes more time – amaurya123 Mar 07 '17 at 13:42
  • @efekctive trying with big set of data now – amaurya123 Mar 07 '17 at 13:42
  • 2
    You aren't doing any kind of processing here - all you are doing is iterating over the array and printing every element. For operations like these it doesn't matter whether the array is sorted – UnholySheep Mar 07 '17 at 13:44

1 Answers1

4

That's simply not how you benchmark code. Run your code a few more times, and you'll see that you get a very different result every time. For instance, here are the results of a few runs on my machine:

Time required by sorted array   4.53115E-4
Time required by unsorted array 2.0451E-4

Time required by sorted array   3.2349E-4
Time required by unsorted array 2.10212E-4

Time required by sorted array   3.48198E-4
Time required by unsorted array 1.90065E-4

If I reverse the order of print loops, to this:

long start = System.nanoTime();

for(int a:array2){
    System.out.println(a);
}

System.out.println("Time required by unsorted array\t"+(System.nanoTime() - start) / 1000000000.0);

start = System.nanoTime();
for(int a:array1){
    System.out.println(a);
}

System.out.println("Time required by sorted array\t"+(System.nanoTime() - start) / 1000000000.0);

The results also change:

Time required by unsorted array 3.04103E-4
Time required by sorted array   2.23136E-4

Time required by unsorted array 3.55041E-4
Time required by sorted array   1.99188E-4

Time required by unsorted array 3.63783E-4
Time required by sorted array   2.0489E-4

So the time difference has less to do with whether the array is sorted, and more to do with which array is being sorted first.

Proper benchmarking of code, especially in Java is tricky. There are whole tools (and more tools) and methodologies devoted to doing just that.

Malt
  • 28,965
  • 9
  • 65
  • 105