1:sleep ()
The minimum unit of seconds.
When using Sleep/ulseep/select, because the thread will go to sleep state, and then wake up, if the single execution problem is not small, if the number of cycles executed more, then the difference is very large.
2:ulseep ()
The smallest unit of microseconds.
3:select ()
Minimum units of microseconds, which need to be assigned each time they are used in the body.
4:RTC ()
Use the IOCTL control.
5: Examples of use of the above four methods
#include <stdio.h>#include<stdlib.h>#include<string.h>#include<sys/time.h>//Gettimeofday Sleep Usleep#include <sys/types.h>//Open#include <sys/stat.h>//Open#include <fcntl.h>//Open#include <errno.h>//Error#include <unistd.h>//Close#include <linux/rtc.h>//Rtc_commadn Rtc_irqp_set/rtc_pie_on/rtc_pie_off#include <sys/ioctl.h>//IOCTL/**g++-o timer_test time_test.cc*//*defining log Output Macros*/#defineDeb_log (Fmat,...) printf ("%s:%d_%s ()" Fmat "\ n", \__file__, __line__, __function__,# #__VA_ARGS__)/*set the clock frequency, the frequency available range is 2~8192, must be 2 of the N-square, 8192, once about 122 subtle,*/#defineFREQ 8192#defineUsec_per_second 1000000/*calculates the number of cycles based on the input wait time (milliseconds)*/#defineCALC_CNT (millseconds) (Millseconds * 1000.0/usec_per_second * FREQ + 0.5)structtimeval TVs, TVE;voidShowTime (intStartstop,LongSleept,Const Char*msg) { if(1==startstop) {Gettimeofday (&tvs,0);//Record timer start time}Else{gettimeofday (&TVE,0);//Record timer end timeDeb_log ("%s: [%ldus] [%ldus]", MSG, sleept, (Tve.tv_sec-TVS.TV_SEC) *1000000LL+ (Tve.tv_usec-tvs.tv_usec)); }}/** Turn on RTC clock device * FREQ:RTC clock frequency * FD: Read the RTC Clock's FD **/intOPENRTC (intFreqint*FD) { /*turn on the RTC time device*/*FD = open ("/DEV/RTC", o_rdonly); if(*fd <0) {Deb_log ("OPEN/DEV/RTC NG, errno=%d msg=%s", errno, Strerror (errno)); return-1; } /*set RTC clock frequency 2~8192hz with a minimum accuracy of 123 microseconds*/ if(IOCTL (*FD, Rtc_irqp_set, freq) <0) {Deb_log ("IOCTL (Rtc_irqp_set)"); Close (*FD); return-1; } /*Start RTC Clock*/ if(IOCTL (*FD, rtc_pie_on,0) <0) {Deb_log ("IOCTL (rtc_pie_on)"); Close (*FD); return-1; } return 0;}/** Turn off RTC clock device * FD: Read the RTC Clock's FD **/voidCLOSERTC (intFD) { /*Turn off RTC clock timings*/IOCTL (FD, Rtc_pie_off,0); /*turn off the RTC device*/Close (FD);}/*In case of using IOCTL, the timer function*/intRtctimer (intMillseconds,intFD) { intLoopnum =calc_cnt (millseconds); unsignedLongdata =0; for(inti =0; i < Loopnum; i++) { /*Read time = 1 seconds/clock frequency (frequency range 2~8192, minimum accuracy is 123 microseconds)*/ if(Read (FD, &data,sizeof(unsignedLong)) <0) { return-1; } } return 0;}intMainintargcChar*argv[]) { intSleepms =3;//Ms intSleeploop = the; Longsleept = Sleepms * Sleeploop * +; /*############## Sleep ############################*/Sleep (1);//Sleep 1 seconds /*############## use Usleep to do timing #################*/ShowTime (1, Sleept,"Usleep"); for(inti =0; i < Sleeploop; ++i) {usleep (Sleepms* +);//Wait units microseconds} showTime (2, Sleept,"Usleep"); /*############## Use Select to do timing ###################*/ShowTime (1, Sleept,"Select"); structTimeval wait_time; Gettimeofday (&tvs,0); for(inti =0; i < Sleeploop; i++) {wait_time.tv_sec=0;//secondsWait_time.tv_usec = Sleepms * +;//Subtle Select(0, NULL, NULL, NULL, &wait_time);//use Select to wait, ten} showTime (2, Sleept,"Select"); /*############## use real-time clock RTC to do timing ################*/ //turn on the RTC clock intFD; intret = OPENRTC (FREQ, &FD); if(0!=ret) {Deb_log ("OPENRTC Error"); return-1; } /*using RTC clock timings*/ShowTime (1, Sleept,"IOCTL RTC"); for(inti =0; i < Sleeploop; ++i) {rtctimer (Sleepms, FD);/*Call timer function*/} showTime (2, Sleept,"IOCTL RTC"); CLOSERTC (FD); //turn off the RTC clock return 0;}
6: Time difference after execution
Test. cc : 33_showtime () Usleep: [15000000US] [15812772us]test. cc Select : [15000000US] [15795666us]test. cc: 33_showtime () IOCTL RTC: [15000000US] [15259142us]
Four timing modes for Linux C (SLEEP/USLEEP/SELECT/IOCTL)