summaryrefslogtreecommitdiff
path: root/zen/perf.h
diff options
context:
space:
mode:
authorDaniel Wilhelm <daniel@wili.li>2014-04-18 17:15:16 +0200
committerDaniel Wilhelm <daniel@wili.li>2014-04-18 17:15:16 +0200
commitbd6336c629841c6db3a6ca53a936d629d34db53b (patch)
tree3721ef997864108df175ce677a8a7d4342a6f1d2 /zen/perf.h
parent4.0 (diff)
downloadFreeFileSync-bd6336c629841c6db3a6ca53a936d629d34db53b.tar.gz
FreeFileSync-bd6336c629841c6db3a6ca53a936d629d34db53b.tar.bz2
FreeFileSync-bd6336c629841c6db3a6ca53a936d629d34db53b.zip
4.1
Diffstat (limited to 'zen/perf.h')
-rw-r--r--zen/perf.h73
1 files changed, 73 insertions, 0 deletions
diff --git a/zen/perf.h b/zen/perf.h
new file mode 100644
index 00000000..f9970d0a
--- /dev/null
+++ b/zen/perf.h
@@ -0,0 +1,73 @@
+// **************************************************************************
+// * This file is part of the FreeFileSync project. It is distributed under *
+// * GNU General Public License: http://www.gnu.org/licenses/gpl.html *
+// * Copyright (C) 2008-2011 ZenJu (zhnmju123 AT gmx.de) *
+// **************************************************************************
+
+#ifndef DEBUG_PERF_HEADER
+#define DEBUG_PERF_HEADER
+
+#include <sstream>
+#include "win.h" //includes "windows.h"
+
+#ifdef __MINGW32__
+#define DEPRECATED(x) x __attribute__ ((deprecated))
+#elif defined _MSC_VER
+#define DEPRECATED(x) __declspec(deprecated) x
+#endif
+
+
+//two macros for quick performance measurements
+#define PERF_START CpuTimer perfTest;
+#define PERF_STOP perfTest.showResult();
+
+class CpuTimer
+{
+public:
+ class TimerError {};
+
+ DEPRECATED(CpuTimer()) : frequency(), startTime(), resultShown(false)
+ {
+ SetThreadAffinity dummy;
+ if (!::QueryPerformanceFrequency(&frequency)) throw TimerError();
+ if (!::QueryPerformanceCounter (&startTime)) throw TimerError();
+ }
+
+ ~CpuTimer()
+ {
+ if (!resultShown)
+ showResult();
+ }
+
+ void showResult()
+ {
+ SetThreadAffinity dummy;
+ LARGE_INTEGER currentTime = {};
+ if (!::QueryPerformanceCounter(&currentTime)) throw TimerError();
+
+ const long delta = static_cast<long>(1000.0 * (currentTime.QuadPart - startTime.QuadPart) / frequency.QuadPart);
+ std::ostringstream ss;
+ ss << delta << " ms";
+
+ ::MessageBoxA(NULL, ss.str().c_str(), "Timer", 0);
+ resultShown = true;
+
+ if (!::QueryPerformanceCounter(&startTime)) throw TimerError(); //don't include call to MessageBox()!
+ }
+
+private:
+ class SetThreadAffinity
+ {
+ public:
+ SetThreadAffinity() : oldmask(::SetThreadAffinityMask(::GetCurrentThread(), 1)) { if (oldmask == 0) throw TimerError(); }
+ ~SetThreadAffinity() { ::SetThreadAffinityMask(::GetCurrentThread(), oldmask); }
+ private:
+ const DWORD_PTR oldmask;
+ };
+
+ LARGE_INTEGER frequency;
+ LARGE_INTEGER startTime;
+ bool resultShown;
+};
+
+#endif //DEBUG_PERF_HEADER
bgstack15