summaryrefslogtreecommitdiff
path: root/include/gras/chrono.hpp
blob: 52b926bdd9456fd7a1c5333646e3574bcad4e95f (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
116
117
118
119
120
121
// Copyright (C) by Josh Blum. See LICENSE.txt for licensing information.

//Boost chrono has awesome high res timer!
//But its only in very recent boosts,
//so we have this little wrapper...
//now/tps inspired by libnumanuma.

#ifndef INCLUDED_GRAS_CHRONO_HPP
#define INCLUDED_GRAS_CHRONO_HPP

#include <gras/gras.hpp>

namespace gras
{
    typedef long long time_ticks_t;

    //! Get the time now in tick counts
    time_ticks_t time_now(void);

    //! Get the number of ticks per second
    time_ticks_t time_tps(void);

    /*!
     * Timer Accumulate creates a scoped timer object,
     * which adds the time elapsed into the accumulator.
     * Typically, the time elapsed is the absolute
     * destructor time - absolute constructor time.
     * However, the user may call done() to accumulate
     * the time elapsed before the destructor is called.
     */
    struct TimerAccumulate
    {
        //! Create a new timer object given the accumulator
        TimerAccumulate(time_ticks_t &accum);

        //! Destructor accumulates the elapsed time
        ~TimerAccumulate(void);

        //! Accumulate elapsed time before destructor
        void done(void);

        time_ticks_t &accum;
        time_ticks_t start;
        bool is_done;
    };

}

//--------------------------------------------------------------------//
//------------------ implementation details below --------------------//
//--------------------------------------------------------------------//

#if defined(_WIN32) || defined(__WIN32__) || defined(WIN32)

#include <windows.h>

namespace gras
{

    GRAS_FORCE_INLINE time_ticks_t time_now(void)
    {
        LARGE_INTEGER counts;
        QueryPerformanceCounter(&counts);
        return counts.QuadPart;
    }

    GRAS_FORCE_INLINE time_ticks_t time_tps(void)
    {
        LARGE_INTEGER freq;
        QueryPerformanceFrequency(&freq);
        return freq.QuadPart;
    }

} //namespace gras

#else

#include <ctime>

namespace gras
{

    GRAS_FORCE_INLINE time_ticks_t time_now(void)
    {
        struct timespec ts;
        clock_gettime(CLOCK_MONOTONIC, &ts);
        return ts.tv_sec*1000000000UL + ts.tv_nsec;
    }

    GRAS_FORCE_INLINE time_ticks_t time_tps(void)
    {
        return 1000000000UL;
    }

} //namespace gras

#endif

namespace gras
{
    GRAS_FORCE_INLINE TimerAccumulate::TimerAccumulate(time_ticks_t &accum):
        accum(accum),
        start(time_now()),
        is_done(false)
    {
        //NOP
    }

    GRAS_FORCE_INLINE TimerAccumulate::~TimerAccumulate(void)
    {
        if (not is_done) this->done();
    }

    GRAS_FORCE_INLINE void TimerAccumulate::done(void)
    {
        accum += (time_now() - start);
        is_done = true;
    }
}

#endif /*INCLUDED_GRAS_CHRONO_HPP*/