Advertisement
Guest User

Benchmark.h

a guest
Apr 22nd, 2015
729
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.97 KB | None | 0 0
  1. #include <Windows.h>
  2. #include <iostream>
  3.  
  4. #define TO_MilSec(_X_) (_X_*1000)
  5. #define TO_Sec(_X_) _X_
  6. #define TO_MicroSec(_X_) (_X_*1000000)
  7.  
  8. #define TO_KB_CEIL(_X_) ceil(double(_X_) / 1024.0)
  9. #define TO_MB_CEIL(_X_) ceil(TO_KB_CEIL(_X_) / 1024.0)
  10.  
  11. class CPUBenchmark
  12. {
  13.  
  14.     LARGE_INTEGER   _startCounter, _elapsedTicks, _frequency;
  15.     LONGLONG        _lastResultInMilliSec;
  16.  
  17. public:
  18.     CPUBenchmark()
  19.     {
  20.         QueryPerformanceCounter(&_startCounter);
  21.     }
  22.  
  23.  
  24.     _inline void restart()
  25.     {
  26.         QueryPerformanceCounter(&_startCounter);
  27.     }
  28.  
  29.     _inline long long stop()
  30.     {
  31.         LARGE_INTEGER endCounter;
  32.         QueryPerformanceCounter(&endCounter);
  33.         _elapsedTicks.QuadPart = endCounter.QuadPart - _startCounter.QuadPart;
  34.  
  35.         QueryPerformanceFrequency(&_frequency);
  36.         _lastResultInMilliSec = TO_MilSec(_elapsedTicks.QuadPart) / _frequency.QuadPart;
  37.         return _elapsedTicks.QuadPart;
  38.     }
  39.  
  40.     _inline long long getLastResultMilli() const
  41.     {
  42.         return _lastResultInMilliSec;
  43.     }
  44.  
  45.     _inline long long getLastResult() const
  46.     {
  47.         return _elapsedTicks.QuadPart;
  48.     }
  49.  
  50.     _inline void printBench()
  51.     {
  52.         printf_s(">>>> Elapsed Tick: %llld\n"
  53.             ">>>> Elapsed Time: %llldms\n",
  54.             _elapsedTicks.QuadPart, _lastResultInMilliSec);
  55.     }
  56.  };
  57.  
  58.  
  59. ////////////////////////////////////////////////
  60. #include <Psapi.h>
  61. class MemoryBenchmark
  62. {
  63.     ////Memory Benchmark
  64.     MEMORYSTATUSEX _memInfo;
  65.     PROCESS_MEMORY_COUNTERS_EX _pmc;
  66.  
  67.     DWORDLONG _totalVMem, _totalVMemUsed, _totalPhysMem, _totalPhysMemUsed;
  68.     DWORDLONG _totalVMemUsedByMe, _totalPhysMemUsedByMe;
  69. public:
  70.     MemoryBenchmark(){ recalculateMem(); }
  71.  
  72.     _inline void recalculateMem()
  73.     {
  74.         getTotalMem(); getCurrentMemByme();
  75.     }
  76.  
  77.     _inline void getTotalMem()
  78.     {
  79.         _memInfo.dwLength = sizeof(MEMORYSTATUSEX);
  80.         GlobalMemoryStatusEx(&_memInfo);
  81.         _totalVMem = TO_KB_CEIL(_memInfo.ullTotalPageFile);
  82.         _totalVMemUsed = TO_KB_CEIL(_memInfo.ullTotalPageFile - _memInfo.ullAvailPageFile);
  83.  
  84.         _totalPhysMem = TO_KB_CEIL(_memInfo.ullTotalPhys);
  85.         _totalPhysMemUsed = TO_KB_CEIL(_memInfo.ullTotalPhys - _memInfo.ullAvailPhys);
  86.     }
  87.  
  88.     _inline long long getCurrentMemByme()
  89.     {
  90.         GetProcessMemoryInfo(GetCurrentProcess(), (PROCESS_MEMORY_COUNTERS*)&_pmc, sizeof(_pmc));
  91.  
  92.         _totalVMemUsedByMe = TO_KB_CEIL(_pmc.PrivateUsage);
  93.         _totalPhysMemUsedByMe = TO_KB_CEIL(_pmc.WorkingSetSize);
  94.         return _totalVMemUsedByMe + _totalPhysMemUsedByMe;
  95.     }
  96.  
  97.     _inline void printTotalMemory()
  98.     {
  99.         printf_s(">>>> Total Virutal Mem: %llldKB\n"
  100.                  ">>>> Total Physcal Mem: %llldKB\n"
  101.                  ">>>> Total Used VM Mem: %llldKB\n"
  102.                  ">>>> Total Used Ph Mem: %llldKB\n"
  103.                  , _totalVMem, _totalPhysMem, _totalVMemUsed, _totalPhysMemUsed);
  104.     }
  105.  
  106.     _inline void printMemoryByMe()
  107.     {
  108.         printf_s(">>>> Used VirMem by Me: %llldKB\n"
  109.                  ">>>> Used Phymem by Me: %llldKB\n"
  110.                 ,_totalVMemUsedByMe, _totalPhysMemUsedByMe);
  111.     }
  112.  
  113.     _inline long long getVMUsedByMe() const
  114.     {
  115.         return _totalVMemUsedByMe;
  116.     }
  117.  
  118.     _inline long long getPhysUsedByMe() const
  119.     {
  120.         return _totalPhysMemUsedByMe;
  121.     }
  122. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement