Listen to people say gettimeofday in 64bit cache, fast, test under, feel wrong ah.
#include <time.h>#include<sys/time.h>#include<stdio.h>#include<stdint.h>intFoointi) { returni;}Constint64_t Max_count =100000* +;structtimereval {timereval (Const Char*module) {Start_time_=Time (NULL); Module_=module; } ~Timereval () {time_t end_time=Time (NULL); printf ("%s\telapse:%d sec\n", Module_, (End_time-start_time_)); } time_t start_time_; Const Char*Module_;};intMain () {structTimeval tptmp; printf ("repeat%d times, test result is: \ n", Max_count); {Timereval eval (" Call Fun"); for(intI=0; i<max_count; ++i) foo (i); } {timereval eval (" Call time"); for(intI=0; i<max_count; ++i) time (NULL);; } {timereval eval ("Call gettimeofday"); for(intI=0; i<max_count; ++i) gettimeofday (&tptmp, NULL);; } {timereval eval ("Call clock_gettime"); structTIMESPEC TP; for(intI=0; i<max_count; ++i) clock_gettime (Clock_realtime,&tp); } return 0;}
Test results
Repeat 100000000 times, test result is:
Call Fun elapse:1 sec
Call Time Elapse:1 sec
Call Gettimeofday elapse:7 sec
Call Clock_gettime elapse:15 sec
Compile parameters
g++ TIMER_BENCHMARCK.CC-M64-LRT
It seems that the fact may not be so, ask for the people, what may be the reason.
If time is only gettimeofday on the basis of a layer of encapsulation, then how time will be faster than the gettimeofday, unscientific Ah!
/*Return the current time as a ' time_t ' and also put it in *t if T was not NULL. Time is represented as seconds from Jan 1 00:00:00 1970. */time_ttime (t) time_t*T; { structTimeval TV; time_t result; if(__gettimeofday (&TV, (structTimeZone *) (NULL)) result= (time_t)-1; Elseresult=(time_t) tv.tv_sec; if(t! =NULL)*t =result; returnresult;}
[email protected] performance evaluation of acquisition time