Tutorial Addendum on Allocation - Allocation API in Java

 31 December 18:00   

    



    



    

SortTest.java - My Testing Program

    



    

Here is my analysis program, SortTest.java:

    

 

    

/**

    

* SortTest.java

    

* Absorb (c) 1999 by Dr. Yang

    

*/

    

import java.util.*;

    

public chic SortTest {

    

changeless int arraySize = 10;

    

changeless HyObject[] dataArray;

    

changeless int numberOfTests = 1000;

    

accessible changeless abandoned main(String[] arg) {

    

if (arg.length>0) {

    

arraySize = Integer.parseInt(arg[0]);

    

}

    

dataArray = new HyObject[arraySize];

    

continued sortTime = 0;

    

Date startTime;

    

for (int j=0; j<numberOfTests; j++) {

    

HyObject.setRandom(j);

    

for (int i=0; i<arraySize; i++) {

    

dataArray[i] = new HyObject();

    

}

    

startTime = new Date();

    

HyArrays.mySort(dataArray, 0, arraySize);

    

// Arrays.sort(dataArray, 0, arraySize);

    

sortTime += (new Date()).getTime() - startTime.getTime();

    

// dump(dataArray, 0, arraySize);

    

}

    

sortTime = sortTime/numberOfTests;

    

System.out.println("Array size: "+arraySize);

    

System.out.println("Average allocation time: "+sortTime

    

+" milliseconds");

    

System.out.println("Number of tests: "+numberOfTests);

    

printTimeInfo(sortTime, arraySize);

    

}

    

accessible changeless abandoned printTimeInfo(long t, int n) {

    

bifold nFactor = ((double)t)/(n/1000);

    

bifold nnFactor = ((double)t)/((n/1000)*n);

    

bifold log2n = Math.log((double) n)/Math.log(2.0);

    

bifold ngFactor = ((double)t)/((n/1000)*log2n);

    

System.out.println("Performance: "+nFactor

    

+" O(N) nonaseconds");

    

System.out.println("Performance: "+ngFactor

    

+" O(N*Log2(N)) nonaseconds");

    

System.out.println("Performance: "+nnFactor

    

+" O(N*N) nonaseconds");

    

}

    

accessible changeless abandoned dump(Object[] a, int fromIndex, int toIndex) {

    

for (int i=fromIndex; i<toIndex; i++) {

    

System.out.println("a["+i+"]: "+a[i].toString());

    

}

    

}

    

}

    



    



    

Note that:

    



        

  • I am repeating the allocation alarm 1000 times with about generated data

        

    elements anniversary to compute the boilerplate allocation time.

        



  •     

  • My own implementations of allocation algorithms are amid in HyArrays class.

        

    mySort() will be replaced by the actualy adjustment name of my implementations.

        



  •     

  • java.util.Arrays.sort() is a JDK accomplishing of Quicksort algorithm.

        

    It is included, but commented out, for allegory purpose.

        



  •     

  • The dump() adjustment is provided just in case you wish to verify if the data

        

    elements are absolutely sorted.

        



  •     

  • The printTimeInfo() adjustment shows how the accomplishing performs against

        

    3 barometer scales: O(N), O(N*log2(N)), and O(N*N). I afflicted the assemblage of measure

        

    to nonasecond to bout the akin of today s CPU power.

        



  •     



    



    

 


Tags: system, average, performance, method, public

 arraysize, sorting, double, println, system, dataarray, sorttime, static, numberoftests, sorttest, public, hyobject, method, nonaseconds, performance, starttime, printtimeinfo, , system out, println performance, static void, public static, sorttest java, public static void, average sorting time,

Share Tutorial Addendum on Allocation - Allocation API in Java:
Digg it!   Google Bookmarks   Del.icio.us   Yahoo! MyWeb   Furl  Binklist   Reddit!   Stumble Upon   Technorati   Windows Live   Bookmark

Text link code :
Hyper link code:

Also see ...

Permalink
Article In : Computers & Technology  -  sort