summaryrefslogtreecommitdiff
path: root/shared/perf.h
blob: 1ad4650d108409f6c2f814097a6b487c670d87be (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
// **************************************************************************
// * 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>
//#define WIN32_LEAN_AND_MEAN -> not in a header
/*
#include <windows.h>
#undef max
#undef min
*/
#include <wx/msw/wrapwin.h> //includes "windows.h"


#ifdef __MINGW32__
#define DEPRECATED(x) x __attribute__ ((deprecated))
#elif defined _MSC_VER
#define DEPRECATED(x) __declspec(deprecated) x
#endif

class CpuTimer
{
public:
    class TimerError {};

    DEPRECATED(CpuTimer()) : resultWasShown(false), startTime(), frequency()
    {
        if (!::QueryPerformanceFrequency(&frequency)) throw TimerError();
        if (!::QueryPerformanceCounter  (&startTime)) throw TimerError();
    }

    ~CpuTimer()
    {
        if (!resultWasShown)
            showResult();
    }

    void showResult()
    {
        LARGE_INTEGER currentTime = {};
        if (!::QueryPerformanceCounter(&currentTime)) throw TimerError();

        const long delta = 1000.0 * (currentTime.QuadPart - startTime.QuadPart) / frequency.QuadPart;
        std::ostringstream ss;
        ss << delta << " ms";

        ::MessageBoxA(NULL, ss.str().c_str(), "Timer", 0);
        resultWasShown = true;

        if (!::QueryPerformanceCounter(&startTime)) throw TimerError(); //don't include call to MessageBox()!
    }

private:
    bool resultWasShown;
    LARGE_INTEGER startTime;
    LARGE_INTEGER frequency;
};

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

#endif //DEBUG_PERF_HEADER
bgstack15