- /*
- * @FileName: TEST_SLEEP.C
- * @Author: WZJ
- * @Brief:
- *
- *
- * @History:
- *
- * @Date: February 07, 2012 Tuesday 22:20:00
- *
- */
- #include <stdio.h>
- #include <stdlib.h>
- #include <time.h>
- #include <sys/time.h>
- #include <errno.h>
- #include <string.h>
- #include <unistd.h>
- #include <sys/types.h>
- #include <sys/select.h>
- int main (int argc, char **argv)
- {
- unsigned int ntimetestsec = 0;
- unsigned int ntimetest = 0;
- struct Timeval tvbegin;
- struct Timeval tvnow;
- int ret = 0;
- unsigned int ndelay = 0;
- struct Timeval TV;
- int fd = 1;
- int i = 0;
- struct Timespec req;
- unsigned int delay[20] =
- {500000, 100000, 50000, 10000, 1000, 900, 500, 100, 10, 1, 0};
- int nreduce = 0; //Error
- fprintf (stderr, "%19s%12s%12s%12s\n", "fuction", "Time (USEC)", "Realtime", "reduce");
- fprintf (stderr, "----------------------------------------------------\ n");
- For (i = 0; i <; i++)
- {
- if (Delay[i] <= 0)
- Break ;
- Ndelay = Delay[i];
- //test Sleep
- Gettimeofday (&tvbegin, NULL);
- ret = Usleep (ndelay);
- if (ret = =-1)
- {
- fprintf (stderr, "Usleep error, errno=%d [%s]\n", errno, Strerror (errno));
- }
- Gettimeofday (&tvnow, NULL);
- Ntimetest = (tvnow.tv_sec-tvbegin.tv_sec) * 1000000 + tvnow.tv_usec-tvbegin.tv_usec;
- Nreduce = Ntimetest-ndelay;
- fprintf (stderr, "\ t usleep%8u%8u%8d\n", Ndelay, Ntimetest,nreduce);
- //test Nanosleep
- Req.tv_sec = ndelay/1000000;
- Req.tv_nsec = (ndelay%1000000) * 1000;
- Gettimeofday (&tvbegin, NULL);
- ret = Nanosleep (&req, NULL);
- if ( -1 = = ret)
- {
- fprintf (stderr, "\ t nanousleep%8u not support\n", ndelay);
- }
- Gettimeofday (&tvnow, NULL);
- Ntimetest = (tvnow.tv_sec-tvbegin.tv_sec) * 1000000 + tvnow.tv_usec-tvbegin.tv_usec;
- Nreduce = Ntimetest-ndelay;
- fprintf (stderr, "\ t nanosleep%8u%8u%8d\n", Ndelay, Ntimetest,nreduce);
- //test Select
- tv.tv_sec = 0;
- Tv.tv_usec = Ndelay;
- Gettimeofday (&tvbegin, NULL);
- ret = SELECT (0, NULL, NULL, NULL, &TV);
- if ( -1 = = ret)
- {
- fprintf (stderr, "select Error. errno =%d [%s]\n], errno, strerror (errno));
- }
- Gettimeofday (&tvnow, NULL);
- Ntimetest = (tvnow.tv_sec-tvbegin.tv_sec) * 1000000 + tvnow.tv_usec-tvbegin.tv_usec;
- Nreduce = Ntimetest-ndelay;
- fprintf (stderr, "\ t select%8u%8u%8d\n", Ndelay, Ntimetest,nreduce);
- //pselcet
- Req.tv_sec = ndelay/1000000;
- Req.tv_nsec = (ndelay%1000000) * 1000;
- Gettimeofday (&tvbegin, NULL);
- ret = pselect (0, NULL, NULL, NULL, &REQ, NULL);
- if ( -1 = = ret)
- {
- fprintf (stderr, "select Error. errno =%d [%s]\n], errno, strerror (errno));
- }
- Gettimeofday (&tvnow, NULL);
- Ntimetest = (tvnow.tv_sec-tvbegin.tv_sec) * 1000000 + tvnow.tv_usec-tvbegin.tv_usec;
- Nreduce = Ntimetest-ndelay;
- fprintf (stderr, "\ t pselect%8u%8u%8d\n", Ndelay, Ntimetest,nreduce);
- fprintf (stderr, "--------------------------------\ n");
- }
- return 0;
- }
The greatest benefit of using Select () as a timer with high accuracy requirements is that it does not affect signal processing, thread safety, and accuracy. In this experiment, when the time delay time is longer, select and Pselect performance is poor, when the time is less than 1 milliseconds, their accuracy increased, performance and Usleep, Nanosleep, and sometimes even more accurate than the latter.
A microsecond level timer under Linux