summaryrefslogtreecommitdiff
path: root/zen/perf.h
blob: 2ebf1955000f4569d2dd0e4b123ce6642d18cf69 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
// *****************************************************************************
// * This file is part of the FreeFileSync project. It is distributed under    *
// * GNU General Public License: https://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 "scope_guard.h"
#include "string_tools.h"

    #include <iostream>


//############# two macros for quick performance measurements ###############
#define PERF_START zen::PerfTimer perfTest;
#define PERF_STOP  perfTest.showResult();
//###########################################################################

/* Example: Aggregated function call time:

    static zen::PerfTimer perfTest(true); //startPaused
    perfTest.resume();
    ZEN_ON_SCOPE_EXIT(perfTest.pause());                      */

namespace zen
{
/* issue with wxStopWatch? https://freefilesync.org/forum/viewtopic.php?t=1426
     - wxStopWatch implementation uses QueryPerformanceCounter: https://github.com/wxWidgets/wxWidgets/blob/17d72a48ffd4d8ff42eed070ac48ee2de50ceabd/src/common/stopwatch.cpp
     - whatever the problem was, it's almost certainly not caused by QueryPerformanceCounter():
          MSDN: "How often does QPC roll over? Not less than 100 years from the most recent system boot"
          https://docs.microsoft.com/en-us/windows/win32/sysinfo/acquiring-high-resolution-time-stamps#general-faq-about-qpc-and-tsc

     - using the system clock is problematic: https://freefilesync.org/forum/viewtopic.php?t=5280

       std::chrono::system_clock wraps ::GetSystemTimePreciseAsFileTime()
       std::chrono::steady_clock wraps ::QueryPerformanceCounter()                       */
class StopWatch
{
public:
    explicit StopWatch(bool startPaused = false) : paused_(startPaused) {}

    bool isPaused() const { return paused_; }

    void pause()
    {
        if (!paused_)
        {
            paused_ = true;
            elapsedUntilPause_ += std::chrono::steady_clock::now() - startTime_;
        }
    }

    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();
    }

    std::chrono::nanoseconds elapsed() const
    {
        auto elapsedTotal = elapsedUntilPause_;
        if (!paused_)
            elapsedTotal += std::chrono::steady_clock::now() - startTime_;
        return elapsedTotal;
    }

private:
    bool paused_;
    std::chrono::steady_clock::time_point startTime_ = std::chrono::steady_clock::now();
    std::chrono::nanoseconds elapsedUntilPause_{}; //std::chrono::duration is uninitialized by default! WTF! When will this stupidity end???
};


class PerfTimer
{
public:
    [[deprecated]] explicit PerfTimer(bool startPaused = false) : watch_(startPaused) {}

    ~PerfTimer() { if (!resultShown_) showResult(); }

    void pause () { watch_.pause(); }
    void resume() { watch_.resume(); }

    void showResult()
    {
        const bool wasRunning = !watch_.isPaused();
        if (wasRunning) watch_.pause(); //don't include call to MessageBox()!
        ZEN_ON_SCOPE_EXIT(if (wasRunning) watch_.resume());

        const int64_t timeMs = std::chrono::duration_cast<std::chrono::milliseconds>(watch_.elapsed()).count();
        const std::string msg = numberTo<std::string>(timeMs) + " ms";
        std::clog << "Perf: duration: " << msg << '\n';
        resultShown_ = true;
    }

private:
    StopWatch watch_;
    bool resultShown_ = false;
};
}

#endif //PERF_H_83947184145342652456
bgstack15