High precision time under Linux

Source: Internet
Author: User
Tags usleep

Today in the company code to see the use of the time-out function of the Select function as the use of timers, and then organized the following several micro-level Linux timer. In my Ubutu10.10 dual-core environment, the compilation passes.

[CPP]View PlainCopy
  1. /*
  2. * @FileName: TEST_SLEEP.C
  3. * @Author: WZJ
  4. * @Brief:
  5. *
  6. *
  7. * @History:
  8. *
  9. * @Date: February 07, 2012 Tuesday 22:20:00
  10. *
  11. */
  12. #include <stdio.h>
  13. #include <stdlib.h>
  14. #include <time.h>
  15. #include <sys/time.h>
  16. #include <errno.h>
  17. #include <string.h>
  18. #include <unistd.h>
  19. #include <sys/types.h>
  20. #include <sys/select.h>
  21. int main (int argc, char **argv)
  22. {
  23. unsigned int ntimetestsec = 0;
  24. unsigned int ntimetest = 0;
  25. struct Timeval tvbegin;
  26. struct Timeval tvnow;
  27. int ret = 0;
  28. unsigned int ndelay = 0;
  29. struct Timeval TV;
  30. int fd = 1;
  31. int i = 0;
  32. struct Timespec req;
  33. unsigned int delay[20] =
  34. {500000, 100000, 50000, 10000, 1000, 900, 500, 100, 10, 1, 0};
  35. int nreduce = 0; //Error
  36. fprintf (stderr, "%19s%12s%12s%12s\n", "fuction", "Time (USEC)", "Realtime", "reduce");
  37. fprintf (stderr, "----------------------------------------------------\ n");
  38. For (i = 0; i <; i++)
  39. {
  40. if (Delay[i] <= 0)
  41. Break ;
  42. Ndelay = Delay[i];
  43. //test Sleep
  44. Gettimeofday (&tvbegin, NULL);
  45. ret = Usleep (ndelay);
  46. if (ret = =-1)
  47. {
  48. fprintf (stderr, "Usleep error, errno=%d [%s]\n", errno, Strerror (errno));
  49. }
  50. Gettimeofday (&tvnow, NULL);
  51. Ntimetest = (tvnow.tv_sec-tvbegin.tv_sec) * 1000000 + tvnow.tv_usec-tvbegin.tv_usec;
  52. Nreduce = Ntimetest-ndelay;
  53. fprintf (stderr, "\ t usleep%8u%8u%8d\n", Ndelay, Ntimetest,nreduce);
  54. //test Nanosleep
  55. Req.tv_sec = ndelay/1000000;
  56. Req.tv_nsec = (ndelay%1000000) * 1000;
  57. Gettimeofday (&tvbegin, NULL);
  58. ret = Nanosleep (&req, NULL);
  59. if ( -1 = = ret)
  60. {
  61. fprintf (stderr, "\ t nanousleep%8u not support\n", ndelay);
  62. }
  63. Gettimeofday (&tvnow, NULL);
  64. Ntimetest = (tvnow.tv_sec-tvbegin.tv_sec) * 1000000 + tvnow.tv_usec-tvbegin.tv_usec;
  65. Nreduce = Ntimetest-ndelay;
  66. fprintf (stderr, "\ t nanosleep%8u%8u%8d\n", Ndelay, Ntimetest,nreduce);
  67. //test Select
  68. tv.tv_sec = 0;
  69. Tv.tv_usec = Ndelay;
  70. Gettimeofday (&tvbegin, NULL);
  71. ret = SELECT (0, NULL, NULL, NULL, &TV);
  72. if ( -1 = = ret)
  73. {
  74. fprintf (stderr, "select Error.  errno =%d [%s]\n], errno, strerror (errno));
  75. }
  76. Gettimeofday (&tvnow, NULL);
  77. Ntimetest = (tvnow.tv_sec-tvbegin.tv_sec) * 1000000 + tvnow.tv_usec-tvbegin.tv_usec;
  78. Nreduce = Ntimetest-ndelay;
  79. fprintf (stderr, "\ t select%8u%8u%8d\n", Ndelay, Ntimetest,nreduce);
  80. //pselcet
  81. Req.tv_sec = ndelay/1000000;
  82. Req.tv_nsec = (ndelay%1000000) * 1000;
  83. Gettimeofday (&tvbegin, NULL);
  84. ret = pselect (0, NULL, NULL, NULL, &REQ, NULL);
  85. if ( -1 = = ret)
  86. {
  87. fprintf (stderr, "select Error.  errno =%d [%s]\n], errno, strerror (errno));
  88. }
  89. Gettimeofday (&tvnow, NULL);
  90. Ntimetest = (tvnow.tv_sec-tvbegin.tv_sec) * 1000000 + tvnow.tv_usec-tvbegin.tv_usec;
  91. Nreduce = Ntimetest-ndelay;
  92. fprintf (stderr, "\ t pselect%8u%8u%8d\n", Ndelay, Ntimetest,nreduce);
  93. fprintf (stderr, "--------------------------------\ n");
  94. }
  95. return 0;
  96. }

The boss suggested that we use Select () as a timer in the case of high precision requirements, the greatest benefit is that it will not affect signal processing, thread safety, and accuracy can be guaranteed. 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 is improved, performance is comparable with Usleep, nanosleep, sometimes even more accurate than the latter

High precision time under Linux

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.