From 9d071d2a2cec9a7662a02669488569a017f0ea35 Mon Sep 17 00:00:00 2001 From: Daniel Wilhelm Date: Mon, 13 Feb 2017 21:25:04 -0700 Subject: 8.9 --- zen/perf.h | 177 +++++++++++++++++++++++++++++-------------------------------- 1 file changed, 83 insertions(+), 94 deletions(-) mode change 100644 => 100755 zen/perf.h (limited to 'zen/perf.h') diff --git a/zen/perf.h b/zen/perf.h old mode 100644 new mode 100755 index 6cd874b8..ada59691 --- 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 -#include "deprecate.h" -#include "scope_guard.h" - -#ifdef ZEN_WIN - #include - #include "win.h" -#else - #include -#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(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 +#include "deprecate.h" +#include "scope_guard.h" + + #include + + +//############# 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(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 -- cgit