fork download
  1.  
  2. #include <iostream>
  3. #include <iomanip>
  4. #include <windows.h>
  5.  
  6. using namespace std;
  7.  
  8. unsigned const gc_Rows = 1000, gc_Cols = 1000, gc_LoopCount = 2000;
  9. unsigned g_Data [gc_Rows][gc_Cols];
  10.  
  11. double TimerInvFrequency ()
  12. {
  13. LARGE_INTEGER q;
  14. QueryPerformanceFrequency (&q);
  15. return 1.0 / q.QuadPart;
  16. }
  17.  
  18. double const gc_InvFreq = TimerInvFrequency ();
  19.  
  20. double ReadTime ()
  21. {
  22. LARGE_INTEGER q;
  23. QueryPerformanceCounter (&q);
  24. return q.QuadPart * gc_InvFreq;
  25. }
  26.  
  27. #define _BENCHMARK_BEGIN() \
  28. { \
  29. double _min = 1000, _max = 0, _total = 0; \
  30. for (unsigned n = 0; n < gc_LoopCount; ++n) \
  31. { \
  32. double t0 = ReadTime (); \
  33.  
  34.  
  35. #define _BENCHMARK_END(case_name) \
  36. double dt = ReadTime() - t0; \
  37. \
  38. _total += dt; \
  39. if (dt < _min) _min = dt; \
  40. if (dt > _max) _max = dt; \
  41. } \
  42. cout << case_name << ": " \
  43. << "min:" << fixed << setprecision(6) << _min \
  44. << ", " << "avg:" << fixed << setprecision(6) << (_total / gc_LoopCount) \
  45. << ", " << "max:" << fixed << setprecision(6) << _max \
  46. << endl; \
  47. } \
  48.  
  49.  
  50. int main ()
  51. {
  52. // Fill the data arrays
  53. for (unsigned i = 0, v = 0; i < gc_Rows; ++i)
  54. for (unsigned j = 0; j < gc_Cols; ++j, ++v)
  55. g_Data[i][j] = v;
  56.  
  57. // Simple iteration
  58. _BENCHMARK_BEGIN()
  59. volatile unsigned sum = 0;
  60. for (unsigned i = 0; i < gc_Rows; ++i)
  61. for (unsigned j = 0; j < gc_Cols; ++j)
  62. sum += g_Data[i][j];
  63. _BENCHMARK_END("Simple iteration (nested loops)")
  64.  
  65. _BENCHMARK_BEGIN()
  66. volatile unsigned sum = 0;
  67. for (unsigned i = 0; i < gc_Rows * gc_Cols; ++i)
  68. sum += g_Data[i / gc_Cols][i % gc_Cols];
  69. _BENCHMARK_END(" Simple iteration (one loop)")
  70.  
  71. _BENCHMARK_BEGIN()
  72. volatile unsigned sum = 0;
  73. unsigned * p = &(g_Data[0][0]);
  74. for (unsigned i = 0; i < gc_Rows * gc_Cols; ++i)
  75. sum += *p++;
  76. _BENCHMARK_END(" Pointer iteration (one loop)")
  77.  
  78. _BENCHMARK_BEGIN()
  79. volatile unsigned sum = 0;
  80. for (auto & i : g_Data)
  81. for (auto j : i)
  82. sum += j;
  83. _BENCHMARK_END(" Range-based for (nested loops)")
  84.  
  85. _BENCHMARK_BEGIN()
  86. volatile unsigned sum = 0;
  87. for (auto const & i : g_Data)
  88. for (auto const & j : i)
  89. sum += j;
  90. _BENCHMARK_END(" Range(const ref)(nested loops)")
  91.  
  92.  
  93. return 0;
  94. }
  95.  
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cpp:4:21: fatal error: windows.h: No such file or directory
compilation terminated.
stdout
Standard output is empty