#include #include #include #include #include using namespace std; // The iteration count below needs to be in a reasonable range // for the specific hardware this program is run on. static const int ITERATIONS = 500000000; // 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 2007-08-22 int main(int argc, char *argv[]){ while(1){ // Do the timing test repeatedly until killed. timeval start, stop; if(gettimeofday(&start, 0) < 0){ // Get starting time. perror("gettimeofday"); exit(1); } for(long i = ITERATIONS; i > 0; i--){ // the actual test // Do nothing. } if(gettimeofday(&stop, 0) < 0){ // Get ending time. perror("gettimeofday"); exit(1); } long diff = stop.tv_sec - start.tv_sec; diff *= 1000000; // microseconds per second diff += stop.tv_usec - start.tv_usec; cout << diff << endl; // Print elapsed time. } }