summaryrefslogtreecommitdiff
path: root/libs/context/performance/zeit.hpp
blob: 2c082bf2208502e3cc238f3f49ccfdfc59cb17e6 (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

//          Copyright Oliver Kowalke 2009.
// Distributed under the Boost Software License, Version 1.0.
//    (See accompanying file LICENSE_1_0.txt or copy at
//          http://www.boost.org/LICENSE_1_0.txt)

#ifndef ZEIT_H
#define ZEIT_H

#include <time.h>

#include <algorithm>
#include <numeric>
#include <cstddef>
#include <vector>

#include <boost/assert.hpp>
#include <boost/bind.hpp>
#include <boost/cstdint.hpp>

typedef boost::uint64_t zeit_t;

inline
zeit_t zeit()
{
    timespec t;
    ::clock_gettime( CLOCK_PROCESS_CPUTIME_ID, & t);
    return t.tv_sec * 1000000000 + t.tv_nsec;
}

struct measure_zeit
{
    zeit_t operator()()
    {
        zeit_t start( zeit() );
        return zeit() - start;
    }
};

inline
zeit_t overhead_zeit()
{
    std::size_t iterations( 10);
    std::vector< zeit_t >  overhead( iterations, 0);
    for ( std::size_t i( 0); i < iterations; ++i)
        std::generate(
            overhead.begin(), overhead.end(),
            measure_zeit() );
    BOOST_ASSERT( overhead.begin() != overhead.end() );
    return std::accumulate( overhead.begin(), overhead.end(), 0) / iterations;
}

#endif // ZEIT_H