Performance evaluation of LINUX@64 acquisition time

Source: Internet
Author: User

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 (&AMP;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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.