fork download
  1. #include <cstdio>
  2. #include <iostream>
  3. #include <map>
  4. #include <limits>
  5.  
  6. #include "windows.h"
  7.  
  8. using namespace std;
  9.  
  10. int main()
  11. {
  12. LARGE_INTEGER tmp_buffer;
  13.  
  14. double freq;
  15. double ns_per_tick;
  16.  
  17. if (!QueryPerformanceFrequency(&tmp_buffer))
  18. cout << "QueryPerformanceFrequency failed\n";
  19.  
  20. freq = (double)tmp_buffer.QuadPart;
  21. ns_per_tick = 1E9 / freq;
  22. cout << "cpu freq = " << ns_per_tick << "ns" << endl;
  23.  
  24. int count = 1000 * 1000;
  25. map<__int64, int> ccnt;
  26. __int64 ft_wait;
  27. __int64 min_wait = LLONG_MAX;
  28. __int64 max_wait = LLONG_MIN;
  29.  
  30. cout << "Wait time (ns): ";
  31. cin >> ft_wait;
  32. ft_wait /= ns_per_tick;
  33.  
  34. for (size_t i = 0; i < count; i++)
  35. {
  36. __int64 ft_beg, ft_end;
  37. __int64 ft_current;
  38.  
  39. QueryPerformanceCounter(&tmp_buffer);
  40. ft_beg = tmp_buffer.QuadPart;
  41.  
  42. do
  43. {
  44. QueryPerformanceCounter(&tmp_buffer);
  45. ft_current = tmp_buffer.QuadPart;
  46. } while ((ft_current - ft_beg) < ft_wait);
  47.  
  48. ft_end = ft_current;
  49. __int64 ft_real_wait = ft_end - ft_beg;
  50.  
  51. min_wait = min_wait > ft_real_wait ? ft_real_wait : min_wait;
  52. max_wait = max_wait < ft_real_wait ? ft_real_wait : max_wait;
  53.  
  54. if (ccnt.find(ft_real_wait) != ccnt.end())
  55. ccnt[ft_real_wait]++;
  56. else
  57. ccnt.insert(pair<__int64, int>(ft_real_wait, 1));
  58. }
  59.  
  60. for (auto i = ccnt.begin(); i != ccnt.end(); ++i)
  61. cout << "計時長度" << (int)(i->first * ns_per_tick) << "發生 " << i->second << " 次" << endl;
  62.  
  63. cout << "計數次數" << count << " 次" << endl;
  64. cout << "最小計時長度" << min_wait * ns_per_tick << "ns" << endl;
  65. cout << "最大計時長度" << max_wait * ns_per_tick << "ns" << endl;
  66.  
  67. getchar();
  68. getchar();
  69. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cpp:6:10: fatal error: windows.h: No such file or directory
 #include "windows.h"
          ^~~~~~~~~~~
compilation terminated.
stdout
Standard output is empty