From 01eb8253196672c969a39587e90b49321a182428 Mon Sep 17 00:00:00 2001 From: Daniel Wilhelm Date: Fri, 18 Apr 2014 17:19:14 +0200 Subject: 5.1 --- zen/perf.h | 69 +++++++++++++++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 62 insertions(+), 7 deletions(-) (limited to 'zen/perf.h') diff --git a/zen/perf.h b/zen/perf.h index 874e6093..ea8991a8 100644 --- a/zen/perf.h +++ b/zen/perf.h @@ -7,14 +7,21 @@ #ifndef DEBUG_PERF_HEADER #define DEBUG_PERF_HEADER -#include #include "deprecate.h" + +#ifdef FFS_WIN +#include #include "win.h" //includes "windows.h" +#else +#include +#include +#endif //two macros for quick performance measurements #define PERF_START CpuTimer perfTest; #define PERF_STOP perfTest.showResult(); +#ifdef FFS_WIN class CpuTimer { public: @@ -24,8 +31,10 @@ public: CpuTimer() : frequency(), startTime(), resultShown(false) { SetThreadAffinity dummy; - if (!::QueryPerformanceFrequency(&frequency)) throw TimerError(); - if (!::QueryPerformanceCounter (&startTime)) throw TimerError(); + if (!::QueryPerformanceFrequency(&frequency)) + throw TimerError(); + if (!::QueryPerformanceCounter (&startTime)) + throw TimerError(); } ~CpuTimer() @@ -38,16 +47,18 @@ public: { SetThreadAffinity dummy; LARGE_INTEGER currentTime = {}; - if (!::QueryPerformanceCounter(¤tTime)) throw TimerError(); + if (!::QueryPerformanceCounter(¤tTime)) + throw TimerError(); - const long delta = static_cast(1000.0 * (currentTime.QuadPart - startTime.QuadPart) / frequency.QuadPart); + const auto delta = static_cast(1000.0 * (currentTime.QuadPart - startTime.QuadPart) / frequency.QuadPart); std::ostringstream ss; ss << delta << " ms"; - ::MessageBoxA(NULL, ss.str().c_str(), "Timer", 0); + ::MessageBoxA(nullptr, ss.str().c_str(), "Timer", 0); resultShown = true; - if (!::QueryPerformanceCounter(&startTime)) throw TimerError(); //don't include call to MessageBox()! + if (!::QueryPerformanceCounter(&startTime)) + throw TimerError(); //don't include call to MessageBox()! } private: @@ -57,6 +68,8 @@ private: SetThreadAffinity() : oldmask(::SetThreadAffinityMask(::GetCurrentThread(), 1)) { if (oldmask == 0) throw TimerError(); } ~SetThreadAffinity() { ::SetThreadAffinityMask(::GetCurrentThread(), oldmask); } private: + SetThreadAffinity(const SetThreadAffinity&); + SetThreadAffinity& operator=(const SetThreadAffinity&); const DWORD_PTR oldmask; }; @@ -65,4 +78,46 @@ private: bool resultShown; }; + +#else +class CpuTimer +{ +public: + class TimerError {}; + + ZEN_DEPRECATE + CpuTimer() : startTime(), resultShown(false) + { + //clock() seems to give grossly incorrect results: multi core issue? + //gettimeofday() seems fine but is deprecated + if (::clock_gettime(CLOCK_MONOTONIC_RAW, &startTime) != 0) //CLOCK_MONOTONIC measures time reliably across processors! + throw TimerError(); + } + + ~CpuTimer() + { + if (!resultShown) + showResult(); + } + + void showResult() + { + timespec currentTime = {}; + if (::clock_gettime(CLOCK_MONOTONIC_RAW, ¤tTime) != 0) + throw TimerError(); + + const auto delta = static_cast((currentTime.tv_sec - startTime.tv_sec) * 1000.0 + (currentTime.tv_nsec - startTime.tv_nsec) / 1000000.0); + std::clog << "Perf: duration: " << delta << " ms\n"; + resultShown = true; + + if (::clock_gettime(CLOCK_MONOTONIC_RAW, &startTime) != 0) + throw TimerError(); + } + +private: + timespec startTime; + bool resultShown; +}; +#endif + #endif //DEBUG_PERF_HEADER -- cgit