#include #include #define micro_second_multiplier 1000000 #include //for gettimeofday #include #include void work() { //Work to be timed for (int ii=0; ii<10; ii++) { for (int i=0; i<10000000; i++) { int j=i*i; } } // end work } void t1() { clock_t start, end; start = clock(); work(); end = clock(); float cpu_time_used = ((float) (end - start)) / CLOCKS_PER_SEC; printf("T1: %10.6f\n", cpu_time_used ); } void t2() { struct timeval begin, end; gettimeofday(&begin, NULL); work(); gettimeofday(&end, NULL); float interval=(float)(end.tv_sec*micro_second_multiplier+end.tv_usec - begin.tv_sec*micro_second_multiplier-begin.tv_usec); printf("T2: %10.2f\n", interval ); } double t2d(struct rusage buf) { return (double)(buf.ru_utime.tv_sec + (double) buf.ru_utime.tv_usec / (double) CLOCKS_PER_SEC); } void t4() { struct rusage buf; getrusage(RUSAGE_SELF, &buf); double startt=t2d(buf); work(); getrusage(RUSAGE_SELF, &buf); double endt = t2d(buf); printf("T4 %10.6f\n", (endt-startt)); } int main(void) { for (int i=0; i<5; i++) { t1(); t2(); t4(); } }