/********************************************************** timetest.c Started 6/3/97 by Jeff Ondich Last modified 6/3/97 Thanks to Mike Tie for help finding timing functions. This program tests gettimeofday() and clock(). gettimeofday() is good, because the struct it returns via its first parameter specifies the number of seconds and micro-seconds (millionths of a second) since midnight, January 1, 1970. One micro-second is small, so you get pretty good precision this way. Unfortunately, gettimeofday() is measuring real time, so you'll get widely varying results depending on whether your program gets to use the processor without interruption, or your program has to take turns with other programs. clock() measures the number of "ticks" of processor time your program has used since it started running. That's great. You don't have to worry about other programs interrupting your program and thus inflating your measured time. On the other hand, one "tick" is equal to 1/64th of a second, which can be a huge amount of time when you're trying to determine how long the various parts of your program take to run. ***********************************************************/ #include #include #include void TestGetTimeOfDay( long *s, long *m ); clock_t TestClock( void ); main() { long seconds, microseconds; TestGetTimeOfDay( &seconds, µseconds ); printf( "TestGetTimeOfDay: %ld seconds and %ld microseconds\n", seconds, microseconds ); printf( "Clock: %ld ticks\n", TestClock() ); } void TestGetTimeOfDay( long *s, long *m ) { int i; struct timeval startTime, endTime; struct timezone zone; gettimeofday( &startTime, &zone ); for( i=0; i < 10000000; i++ ) ; gettimeofday( &endTime, &zone ); *s = endTime.tv_sec - startTime.tv_sec; *m = endTime.tv_usec - startTime.tv_usec; if( *m < 0 ) { *m = 1000000 + *m; (*s)--; } return; } clock_t TestClock( void ) { int i; clock_t startTime = clock(); for( i=0; i < 1000000; i++ ) ; return( clock() - startTime ); }