#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. If you make a change to // the inner loop that makes it slower (as assigned in the lab), you // may also want to adjust the iteration count downward. static const int ITERATIONS = 1000000; // This program runs furiously away in a loop, writing a character to // a temporary file each time around the loop. 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. -Max Hailperin // 2007-08-22 int main(int argc, char *argv[]){ FILE *outFile; // temporary output file if((outFile = tmpfile()) == NULL){ cerr << "Couldn't open temporary file." << endl; exit(1); } int outFd = fileno(outFile); // convert to system-level file desciptor 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 char c = 'X'; // arbitrary character to write if(pwrite(outFd, &c, 1, 0) < 0){ // Write the 'X' out from c to temp file. perror("pwrite"); } } 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. } }