diff options
author | Daniel Wilhelm <shieldwed@outlook.com> | 2017-02-13 21:25:04 -0700 |
---|---|---|
committer | Daniel Wilhelm <shieldwed@outlook.com> | 2017-02-13 21:25:04 -0700 |
commit | 9d071d2a2cec9a7662a02669488569a017f0ea35 (patch) | |
tree | c83a623fbdff098339b66d21ea2e81f3f67344ae /zen/perf.h | |
parent | 8.8 (diff) | |
download | FreeFileSync-9d071d2a2cec9a7662a02669488569a017f0ea35.tar.gz FreeFileSync-9d071d2a2cec9a7662a02669488569a017f0ea35.tar.bz2 FreeFileSync-9d071d2a2cec9a7662a02669488569a017f0ea35.zip |
8.9
Diffstat (limited to 'zen/perf.h')
-rwxr-xr-x[-rw-r--r--] | zen/perf.h | 177 |
1 files changed, 83 insertions, 94 deletions
diff --git a/zen/perf.h b/zen/perf.h index 6cd874b8..ada59691 100644..100755 --- a/zen/perf.h +++ b/zen/perf.h @@ -1,94 +1,83 @@ -// ***************************************************************************** -// * This file is part of the FreeFileSync project. It is distributed under * -// * GNU General Public License: http://www.gnu.org/licenses/gpl-3.0 * -// * Copyright (C) Zenju (zenju AT freefilesync DOT org) - All Rights Reserved * -// ***************************************************************************** - -#ifndef PERF_H_83947184145342652456 -#define PERF_H_83947184145342652456 - -#include <chrono> -#include "deprecate.h" -#include "scope_guard.h" - -#ifdef ZEN_WIN - #include <sstream> - #include "win.h" -#else - #include <iostream> -#endif - - -//############# two macros for quick performance measurements ############### -#define PERF_START zen::PerfTimer perfTest; -#define PERF_STOP perfTest.showResult(); -//########################################################################### - -namespace zen -{ -class PerfTimer -{ -public: - ZEN_DEPRECATE PerfTimer() {} - - ~PerfTimer() { if (!resultShown_) showResult(); } - - void pause() - { - if (!paused_) - { - paused_ = true; - elapsedUntilPause_ += std::chrono::steady_clock::now() - startTime_; //ignore potential ::QueryPerformanceCounter() wrap-around! - } - } - - void resume() - { - if (paused_) - { - paused_ = false; - startTime_ = std::chrono::steady_clock::now(); - } - } - - void restart() - { - paused_ = false; - startTime_ = std::chrono::steady_clock::now(); - elapsedUntilPause_ = std::chrono::nanoseconds::zero(); - } - - int64_t timeMs() const - { - auto elapsedTotal = elapsedUntilPause_; - if (!paused_) - elapsedTotal += std::chrono::steady_clock::now() - startTime_; - - return std::chrono::duration_cast<std::chrono::milliseconds>(elapsedTotal).count(); - } - - void showResult() - { - const bool wasRunning = !paused_; - if (wasRunning) pause(); //don't include call to MessageBox()! - ZEN_ON_SCOPE_EXIT(if (wasRunning) resume()); - -#ifdef ZEN_WIN - std::wostringstream ss; - ss << timeMs() << L" ms"; - ::MessageBox(nullptr, ss.str().c_str(), L"Timer", MB_OK); -#else - std::clog << "Perf: duration: " << timeMs() << " ms\n"; -#endif - resultShown_ = true; - } - -private: - bool resultShown_ = false; - bool paused_ = false; - std::chrono::steady_clock::time_point startTime_ = std::chrono::steady_clock::now(); //uses ::QueryPerformanceCounter() - std::chrono::nanoseconds elapsedUntilPause_{}; //std::chrono::duration is uninitialized by default! WTF! When will this stupidity end??? -}; -} - -#endif //PERF_H_83947184145342652456 +// *****************************************************************************
+// * This file is part of the FreeFileSync project. It is distributed under *
+// * GNU General Public License: http://www.gnu.org/licenses/gpl-3.0 *
+// * Copyright (C) Zenju (zenju AT freefilesync DOT org) - All Rights Reserved *
+// *****************************************************************************
+
+#ifndef PERF_H_83947184145342652456
+#define PERF_H_83947184145342652456
+
+#include <chrono>
+#include "deprecate.h"
+#include "scope_guard.h"
+
+ #include <iostream>
+
+
+//############# two macros for quick performance measurements ###############
+#define PERF_START zen::PerfTimer perfTest;
+#define PERF_STOP perfTest.showResult();
+//###########################################################################
+
+namespace zen
+{
+class PerfTimer
+{
+public:
+ ZEN_DEPRECATE PerfTimer() {}
+
+ ~PerfTimer() { if (!resultShown_) showResult(); }
+
+ void pause()
+ {
+ if (!paused_)
+ {
+ paused_ = true;
+ elapsedUntilPause_ += std::chrono::steady_clock::now() - startTime_; //ignore potential ::QueryPerformanceCounter() wrap-around!
+ }
+ }
+
+ void resume()
+ {
+ if (paused_)
+ {
+ paused_ = false;
+ startTime_ = std::chrono::steady_clock::now();
+ }
+ }
+
+ void restart()
+ {
+ paused_ = false;
+ startTime_ = std::chrono::steady_clock::now();
+ elapsedUntilPause_ = std::chrono::nanoseconds::zero();
+ }
+
+ int64_t timeMs() const
+ {
+ auto elapsedTotal = elapsedUntilPause_;
+ if (!paused_)
+ elapsedTotal += std::chrono::steady_clock::now() - startTime_;
+
+ return std::chrono::duration_cast<std::chrono::milliseconds>(elapsedTotal).count();
+ }
+
+ void showResult()
+ {
+ const bool wasRunning = !paused_;
+ if (wasRunning) pause(); //don't include call to MessageBox()!
+ ZEN_ON_SCOPE_EXIT(if (wasRunning) resume());
+
+ std::clog << "Perf: duration: " << timeMs() << " ms\n";
+ resultShown_ = true;
+ }
+
+private:
+ bool resultShown_ = false;
+ bool paused_ = false;
+ std::chrono::steady_clock::time_point startTime_ = std::chrono::steady_clock::now(); //uses ::QueryPerformanceCounter()
+ std::chrono::nanoseconds elapsedUntilPause_{}; //std::chrono::duration is uninitialized by default! WTF! When will this stupidity end???
+};
+}
+
+#endif //PERF_H_83947184145342652456
|