The accuracy test of Win32 function sleep

Source: Internet
Author: User

In three ways, the first uses high-precision performance counters, the second is the use of multimedia timers, and the other is the CPU cycles provided in Windows graphics programming. The first method of measurement is recommended:

Let's look at the first type:

[CPP]View Plaincopy
  1. #include <windows.h>
  2. #include <stdio.h>
  3. void Main ()
  4. {
  5. Large_integer litmp;
  6. Longlong Qt1,qt2;
  7. double DFT,DFF,DFM;
  8. //Get clock frequency
  9. QueryPerformanceFrequency (&LITMP); //Get clock frequency
  10. dff= (double) litmp.  QuadPart;
  11. //Get initial value
  12. QueryPerformanceCounter (&LITMP);
  13. Qt1=litmp. QuadPart;
  14. //below some time-consuming operations
  15. Sleep (1);
  16. //Get termination value
  17. QueryPerformanceCounter (&LITMP);
  18. Qt2=litmp. QuadPart;
  19. //Get the corresponding time value, go to the millisecond unit
  20. Dfm= (Double) (QT2-QT1);
  21. DFT=DFM/DFF;
  22. printf ("spents:%.3f milliseconds \ n", dft*1000.0);
  23. }

On my machine for Sleep (1) = 0.454ms;sleep (Ten) = 9.719ms;sleep (+) = 99.541ms

Here are two other options for reference:

[CPP]View Plaincopy
  1. #include <stdio.h>
  2. #include <Windows.h>
  3. #include <Mmsystem.h>
  4. #include "Timer.h"
  5. #pragma comment (lib, "Winmm.lib")
  6. int i = 0;
  7. DWORD start;
  8. DWORD end;
  9. #define TIMES (1000)
  10. void Main ()
  11. {
  12. ////////////////////////////////////////////////////////////////////////////////  
  13. //  
  14. //1. Initialize with a high-precision timer
  15. //  
  16. //high-precision timer initialization
  17. //NOTE: If you do not execute the following statement initialized to 1ms, the precision of sleep () will be affected
  18. :: Timebeginperiod (1);
  19. Sleep (100);
  20. //Start timing
  21. Start =:: timeGetTime ();
  22. For (i=0; i<times; i++)//cumulative test
  23. {
  24. Sleep (1);
  25. }
  26. //End Timing
  27. End =:: timeGetTime ();
  28. printf ("Use a high-precision timer to test sleep (1) Time:%.3f ms\n", (End-start)/((Double) times));
  29. ////////////////////////////////////////////////////////////////////////////////  
  30. //  
  31. //2. Using the number of CPU cycles to get
  32. //  
  33. Ktimer timer;
  34. ///below to get CPU speed (MHZ)
  35. Timer. Start ();
  36. Sleep (1000);
  37. unsigned __int64 cpuspeed = (unsigned) (timer.  Stop ()/1000000);
  38. printf ("CPU speed:%i64d mhz\n", cpuspeed);
  39. //Start testing
  40. Timer. Start ();
  41. Sleep (1);
  42. //End
  43. unsigned __int64 time = (unsigned) timer.  Stop ();
  44. printf ("sleep (1) time measured with CPU cycles:%i64dμs\n", time/cpuspeed);
  45. //  
  46. //After the test is completed, the timing accuracy is recalled
  47. //  
  48. :: Timeendperiod (1);
  49. }

The accuracy test of Win32 function sleep

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.