summaryrefslogtreecommitdiff
path: root/zen/perf.h
blob: e52bf662954aeb57ada6a83e6c55d5cd6ee42014 (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
// **************************************************************************
// * 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 gmx DOT de) - All Rights Reserved        *
// **************************************************************************

#ifndef DEBUG_PERF_HEADER_83947184145342652456
#define DEBUG_PERF_HEADER_83947184145342652456

#include "deprecate.h"
#include "tick_count.h"

#ifdef ZEN_WIN
    #include <sstream>
#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:
    class TimerError {};

    ZEN_DEPRECATE
    PerfTimer() : //throw TimerError
        ticksPerSec_(ticksPerSec()),
        resultShown(false),
        startTime(getTicksNow()),
        paused(false),
        elapsedUntilPause(0)
    {
        //std::clock() - "counts CPU time in Linux GCC and wall time in VC++" - WTF!???
        if (ticksPerSec_ == 0)
            throw TimerError();
    }

    ~PerfTimer() { if (!resultShown) try { showResult(); } catch (TimerError&) {} }

    void pause()
    {
        if (!paused)
        {
            paused = true;
            elapsedUntilPause += dist(startTime, getTicksNow());
        }
    }

    void resume()
    {
        if (paused)
        {
            paused = false;
            startTime = getTicksNow();
        }
    }

    void restart()
    {
        startTime = getTicksNow();
        paused = false;
        elapsedUntilPause = 0;
    }

    int64_t timeMs() const
    {
        int64_t ticksTotal = elapsedUntilPause;
        if (!paused)
            ticksTotal += dist(startTime, getTicksNow());
        return 1000 * ticksTotal / ticksPerSec_;
    }

    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:
    TickVal getTicksNow() const
    {
        const TickVal now = getTicks();
        if (!now.isValid())
            throw TimerError();
        return now;
    }

    const std::int64_t ticksPerSec_;
    bool resultShown;
    TickVal startTime;
    bool paused;
    int64_t elapsedUntilPause;
};
}

#endif //DEBUG_PERF_HEADER_83947184145342652456
bgstack15