1

I try below program to check if same algorithm runs few times on same array will it give different time durations for sorting.It returned different times. Why is that ? i used same array for all attempts. package sorting;

import java.util.Arrays;
import static sorting.CompareSorting.bubble_sort;
import static sorting.CompareSorting.generate_data;


public class Sorting {


public static void main(String [] args){

    int size = 10000;
    int [] arr = new int[size];
    int [] arr1 = new int[size];
    int [] arr2 = new int[size];
    long startTime;
    long endTime;

    arr = generate_data(size);

    System.arraycopy(arr, 0, arr1, 0, size);
    System.arraycopy(arr, 0, arr2, 0, size);

    System.out.println(Arrays.toString(arr));
    System.out.println(Arrays.toString(arr1));
    System.out.println(Arrays.toString(arr2));

    startTime = System.currentTimeMillis();
    bubble_sort(arr);
    endTime = System.currentTimeMillis();
    System.out.println("1st attempt " + (endTime - startTime));

    startTime = System.currentTimeMillis();
    bubble_sort(arr1);
    endTime = System.currentTimeMillis();
    System.out.println("2nd attempt " + (endTime - startTime));

    startTime = System.currentTimeMillis();
    bubble_sort(arr2);
    endTime = System.currentTimeMillis();
    System.out.println("3rd attempt " + (endTime - startTime));

    }
}

This is the sorting method

static void bubble_sort(int [] data) { 

    int i,j,tmp;
    for(i = data.length ; i >= 0 ; i-- ){

        for(j = 0 ; j < data.length - 1 ; j++ ){

            if(data[j] > data[j + 1]){

                tmp = data[j];

                data[j] = data[j + 1];

                data[j + 1] = tmp;

            }

        }

    }

}

Returned values : 1st attempt 438 2nd attempt 359 3rd attempt 297

Dinanjana
  • 162
  • 12
  • 3
    It is not a proper way to do benchmarks in Java. You can read more about it here: http://stackoverflow.com/questions/4583175/benchmarking-java-programs. – kraskevich Feb 02 '15 at 15:23
  • (1) You need proper warm up before experiment. (2) JIT compilation is more likely to have already optimized parts of code in the later times it ran (one of the reasons for warm up) (3) One run says nothing, when trying to determine "which is better" empirically - you should use statistical tools, don't try to understand anything before you know the result is [**statistical significant**](http://en.wikipedia.org/wiki/Statistical_significance). – amit Feb 02 '15 at 15:27
  • so compiler optimises and recognize patterns ? So is that the reason for reducing time for every instance i check ? btw thanks kraskevich and amit – Dinanjana Feb 02 '15 at 15:35
  • @Dina JIT is Just In Time compilation, it is implemented by the Virtual machine (that runs the compiled byte code) and tries top optimize pieces of code that are repeating themselves often enough. This is done in runtime. – amit Feb 02 '15 at 15:37
  • Oh i got it now.thanks amit. :) – Dinanjana Feb 02 '15 at 15:39
  • 1
    Running times are nondeterministic on non-hard-real-time systems. Because the system is busy doing many other things at the same time and memory accesses follow an extremely complex non-repeatable behavior. –  Feb 02 '15 at 16:09

1 Answers1

1

Java uses JVM (Jav Virtual Machine) to run the applications we made from Java. It optimize the code once it is ran. Hence, when you trying to sort the array for three or four times, it optimizes the code and hence run time will be reduced. Checkout This, and This

Community
  • 1
  • 1
ThisaruG
  • 3,222
  • 7
  • 38
  • 60