#include #include #include /* The iteration count below needs to be in a reasonable range for the specific hardware this program is run on. */ #define ITERATIONS 100000000 /* This program runs furiously away in a loop, doing nothing in particular, chewing up lots of processor time. It will stop only when interrupted or killed. Every ITERATIONS times through its loop it prints a report of how long (in microseconds) it took to do the last ITERATIONS iterations. The time is elapsed (wall clock) time, so that it includes not only time this process spent, but also time when the processor is busy running other processes. The times are not actually accurate to the microsecond. Important note: when compiling this program, do not tell the compiler to optimize the program, since doing so might eliminate the inner loop entirely. -Max Hailperin 1999-08-30 */ int main(int argc, char *argv[]){ while(1){ /* Do the timing test repeatedly until killed. */ struct timeval start, stop; long diff, i; if(gettimeofday(&start, 0) < 0){ /* Get starting time. */ perror("gettimeofday"); exit(1); } for(i = ITERATIONS; i > 0; i--){ /* the actual test */ /* Do nothing. */ } if(gettimeofday(&stop, 0) < 0){ /* Get ending time. */ perror("gettimeofday"); exit(1); } diff = stop.tv_sec - start.tv_sec; diff *= 1000000; /* microseconds per second */ diff += stop.tv_usec - start.tv_usec; printf("%ld\n", diff); /* Print elapsed time. */ } }