Guest User

Untitled

a guest
Jan 23rd, 2018
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.48 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4. #include <ctype.h>
  5. #include <math.h>
  6. #include <windows.h>
  7. #include <iostream>
  8. #define WIN32_LEAN_AND_MEAN
  9. using namespace std;
  10.  
  11. inline unsigned __int64 RDTSC(void)
  12. {
  13.     _asm  _emit 0x0F
  14.     _asm  _emit 0x31
  15. }
  16.  
  17. class TimerRDTSC
  18. {
  19.     unsigned __int64  start_cycle;
  20.     unsigned __int64  end_cycle;
  21.  
  22. public:
  23.     inline void Start()
  24.     {
  25.         start_cycle = RDTSC();
  26.     }
  27.  
  28.     inline void Stop()
  29.     {
  30.         end_cycle = RDTSC();
  31.     }
  32.  
  33.     unsigned __int64 Interval()
  34.     {
  35.         return end_cycle - start_cycle;
  36.     }
  37. };
  38.  
  39. class TimerPerformanceCounter
  40. {
  41.     unsigned __int64  start_time;
  42.     unsigned __int64  end_time;
  43.  
  44. public:
  45.     inline void Start()
  46.     {
  47.         QueryPerformanceCounter(reinterpret_cast<LARGE_INTEGER*>(&start_time));
  48.     }
  49.  
  50.     inline void Stop()
  51.     {
  52.         QueryPerformanceCounter(reinterpret_cast<LARGE_INTEGER*>(&end_time));
  53.     }
  54.  
  55.     unsigned __int64 Interval()
  56.     {
  57.         // Return duration in seconds...
  58.         return end_time - start_time;
  59.     }
  60. };
  61.  
  62. template<class Timer, class Test, unsigned SleepRepeat, unsigned QuantumRepeat=1>
  63. class ProfileSpeed
  64. {
  65.     unsigned __int64 test_interval;
  66.     Timer            timer;
  67.     Test             test;
  68.  
  69.     void QuantumTest()
  70.     {
  71.         unsigned i;
  72.         Sleep(10);
  73.         for (i=0; i < QuantumRepeat; ++i)
  74.         {
  75.             timer.Start();
  76.             test.RunTest();
  77.             timer.Stop();
  78.             test_interval += timer.Interval();
  79.         }
  80.     }
  81.  
  82. public:
  83.     ProfileSpeed() : test_interval(0) {}
  84.  
  85.     void Run()
  86.     {
  87.         unsigned i;
  88.         for (i=0; i < SleepRepeat; ++i)
  89.         {
  90.             QuantumTest();
  91.         }
  92.     }
  93.  
  94.     unsigned __int64 TestInterval() {return test_interval;}
  95. };
  96.  
  97. class AbsTest
  98. {
  99. public:
  100.    
  101.     inline static void RunTest()
  102.     {
  103.         double heat = -3.55;
  104.         heat = abs(heat);
  105.     }
  106. };
  107. class DirectAssignmentTest
  108. {
  109. public:
  110.    
  111.     inline static void RunTest()
  112.     {
  113.         double heat = -3.55;
  114.          heat = (heat < 0 ? -heat : heat);
  115.     }
  116. };
  117. int main(int argc, char* argv[])
  118. {
  119.     // Run HelloTest 50 times sleeping between each test, using TimerRDTSC
  120.     ProfileSpeed<TimerPerformanceCounter, AbsTest, 50> test1;
  121.  
  122.     // Run HelloTest 50 times sleeping between every 2 tests, using TimerRDTSC
  123.     ProfileSpeed<TimerPerformanceCounter, DirectAssignmentTest, 50, 2> test2;
  124.  
  125.     // Switch test order if an argument is specified to the program
  126.     if (argc == 1)
  127.     {
  128.         test1.Run();
  129.         test2.Run();
  130.     }
  131.     else
  132.     {
  133.         test2.Run();
  134.         test1.Run();
  135.     }
  136.  
  137.     cout << "Test 1 Interval: " << unsigned(test1.TestInterval()) << endl;
  138.     cout << "Test 2 Interval: " << unsigned(test2.TestInterval()) << endl;
  139.  
  140.     return 0;
  141. }
Add Comment
Please, Sign In to add comment