A Note on Timing


For your benchmarks, the platform independent way to go is this:

#include <ctime> // or <time.h> in C
time_t t0= clock() ;
// time-consuming stuff here...
time_t t1= clock() ;
double elapsedSeconds= ( t1 - t0 ) / (double) CLOCKS_PER_SEC ;

Pick your values of n to give times in the ones to tens of seconds (at least); this is not a high resolution counter. Mike Helmick passes on the following link with some code on how to check the effective resolution of clock() : http://www.cis.ksu.edu/~howell/500s00/lab2/stats.c 

If you are using Windows and are not too embarrassed to use a platform-specific counter, the highest resolution is available this way:

#include <windows.h> 
__int64 getTicks()
{
    LARGE_INTEGER  iiTicks ;
    BOOL ok= QueryPerformanceCounter( &iiTicks ) ;
    assert( ok ) ;  
    return (__int64 ) iiTicks.QuadPart ;
}

double getElapsedSeconds( __int64 tStart, __int64 tStop )
{
    LARGE_INTEGER iiTicksPerSec ;
    BOOL ok= QueryPerformanceFrequency( &iiTicksPerSec ) ;
    assert( ok ) ;
    double freq = (double)  (__int64 ) iiTicksPerSec.QuadPart ;
    return ( tStop - tStart ) / freq ;
}
        
int main()
{
    __int64 t0= getTicks() ;
    // time-consuming stuff here
    __int64 t1= getTicks() ;
    double elapsedSeconds= getElapsedSeconds( t1, t0 ) ;
}

... but this is certainly not necessary for these kind of "large n" benchmarks. (I mention it only to satisfy idle curiosity.)


Back to main CSC 601 page.

Last updated: August 29,  2002.