Timing Processes Sample code: Lecture 18

/***************************************
 * 
 * TimingTest: Simple version  
 *
 * Synopsis: Demonstrates how to construct a timing loop in JAva 
             to count how many milliseconds the process takes
	     This is a very simple approach.
	     A more thorough example is below, once you see the basic idea.

 * Author:   Arthur Crummer
 * History:  created 2/9/98 v1.0 
             last update for web page 2/19/98

  YOU SHOULD : Compile and run this program. 
               Study output. Modify program and see new results.
	       Incorporate the time-test strategy in a program of your own 
	       to time some other activities. 
	       Study the program  DaveTimingTest  below.

****************/


import java.util.Date  ;  // for class Date 

class TestTime 
{

  public static void main (String[] args){

    int bigLoopSize   = 10000;
    int smallLoopSize = 1000 ;

    Date start;
    Date stop;   // when these are constructed, the system time 
                 // will be read to within millesecond accuracy

    long timeRequiredToFillArray, timeRequiredToPrintArray ;

    int[] A = new int[bigLoopSize] ;


    start = new Date();                             // read the date&Time before the process
    for( int i = 0 ; i < bigLoopSize; i++ )         
      A[i] = i ;                                    // put in some data
    stop = new Date();                              // read the date&Time after the process

    timeRequiredToFillArray = stop.getTime() - start.getTime();


    start = new Date();
    for( int i = 0 ; i < smallLoopSize ; i++ )  System.out.println(A[i]) ;
    stop = new Date();

    timeRequiredToPrintArray = stop.getTime() - start.getTime();

    System.out.println("To  fill array of size " + bigLoopSize +":"+ 
		       timeRequiredToFillArray +" ms");
    System.out.println("To Print array of size " + smallLoopSize +":"+ 
		       timeRequiredToPrintArray+" ms");

    System.out.println("Times will vary depending on machine and activity level."  );


    Date d = new Date() ;
    System.out.println("Todays date is :" + d.toString()  );
    System.out.println("Todays time is :" + d.getTime()  );

  }
}


/**************************  
Crummer's note: On my machine I got this output, 
after the display of values in the array of course:
...994
995
996
997
998
999
To  fill array of size 10000:4 ms
To Print array of size 1000:266 ms
Times will vary depending on machine and activity level.
Todays date is :Thu Feb 19 20:53:51 EST 1998
Todays time is :887939631609
**************************************************************/







/**************************
 * DavesTimingTest
 *
 * Synopsis: Demonstrates how to construct a timing loop in more detail
 * Author:   Dave Small
 * History:  v1.0 - 9802.15
*/


import java.util.Date;
import ADT.PriorityQueue;

class DavesTimingTest
{
  public static void main( String[] args )
  {
    int SAMPLES = 10000;
    int N = 20;

    Date start;
    Date stop;

    //----------------------------------------------------
    // Create a set of test PQs

    PriorityQueue[] pq = new PriorityQueue[SAMPLES];

    for( int i = 0 ; i < SAMPLES ; i++ )
      pq[i] = new Mystery.PQ();

    //----------------------------------------------------
    // Compute test overhead

    start = new Date();

    for( int i = 0 ; i < SAMPLES ; i++ )
      pq[i].length();

    stop = new Date();

    long overhead = stop.getTime() - start.getTime();

    System.out.println("Overhead = " + overhead );

    //----------------------------------------------------
    // Compute time to insert N items into a PQ

    Integer datum = new Integer( 33 );

    for( int j = 0 ; j < N ; j++ )
      {
        int priority = N-j;

        start = new Date();

        for( int i = 0 ; i < SAMPLES ; i++ )
          pq[i].insert( datum, priority );

        stop = new Date();

        System.out.println("PQ items = " + j +
                           "\tInsert pri = " + priority +
                           "\t   Total time = " +
                           (stop.getTime() - start.getTime()) +
                           "\t Adjusted time = " +
                           (stop.getTime() - start.getTime() - overhead) );
      }
  }
}

  • Back to Java programs list