#include // symbolic constants #include // general utilities #include // printf #include // gettimeofday #include // wait #include // wait // // Purpose: Measure the average execution times of: // o the stub function call // o the system call gettimeofday(2) // o the fork(2) system call // using gettimeofday. Run each case 3 times. // // Usage: timeit > timeit.out # produce raw data // // Output: The output lines are marked with a letter to indicate the // call being timed: // 'S' for the stub function call // 'G' for the gettimeofday call // 'F' for the fork call // For example: // S 10000 0.004400 (1170266760, 781009) (1170266760, 781053) // means that the output is for the stub function call where N = 1000, // tavg = 0.004400 usec. The parenthesized values are the tbegin and tend // values // // Build: g++ -o timeit timeit.c // const int LIMIT = 10000; // limit on N const int M = 1000000; void stub (void) { /* null function */ } static inline pid_t Fork(void) { pid_t pid; if ((pid=fork()) < 0) { perror(""); exit(1); } return pid; } static inline pid_t Waitpid(pid_t pid, int *status, int options) { if (wait(status) != pid) { perror(""); exit(1); } return pid; } int main (int argc, char *argv[]) { enum { STUB=0, GETTOD, FORK }; const char which[3] = { 'S', 'G', 'F' }; setlinebuf(stdout); // VERY important to avoid output confusion system("uptime"); for (int i=0; i<3; i++) { // each call type for (int N=10; N<=LIMIT; N*=10) { // N = 10, 100, ... , LIMIT double tavg; for (int j=0; j<3; j++) { // do 3 times if (i == STUB) { for (int k=0; k