summaryrefslogtreecommitdiffstats
path: root/src/boost/libs/heap/tools/heap_benchmarks.hpp
blob: 08bc65791a46b75fa04445c33cf00fb180cc8290 (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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
/*=============================================================================
    Copyright (c) 2010 Tim Blechmann

    Use, modification and distribution is subject to 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)
=============================================================================*/

#include <algorithm>
#include <vector>

#include <boost/foreach.hpp>
#include "high_resolution_timer.hpp"

#include <boost/heap/heap_merge.hpp>

#if defined(__GNUC__) && (!defined __INTEL_COMPILER)
#define no_inline __attribute__ ((noinline))
#else
#define no_inline
#endif

typedef std::vector<int> test_data;

const int num_benchmarks = 1;

inline test_data make_sequential_test_data(int size)
{
    test_data v(size);
    for (int i = 0; i != size; ++i)
        v[i] = i;
    return v;
}

inline test_data make_test_data(int size)
{
    test_data v = make_sequential_test_data(size);
    std::random_shuffle(v.begin(), v.end());
    return v;
}

const int max_data = 20;

test_data const & get_data(int index)
{
    static std::vector <test_data> data;
    if (data.empty())
        for (int i = 0; i != num_benchmarks; ++i)
            data.push_back(make_test_data((1<<(max_data+1))+1024));

    return data[index];
}


#define DEFINE_BENCHMARKS_SELECTOR(SUFFIX)  \
struct make_##SUFFIX                        \
{                                           \
    template <typename heap>                \
    struct rebind {                         \
        typedef run_##SUFFIX<heap> type;    \
    };                                      \
};


template <typename pri_queue>
void fill_heap(pri_queue & q, int index, int size, int offset = 0)
{
    test_data const & data = get_data(index);

    for (int i = 0; i != size + 1; ++i) {
        q.push(data[i]);
        int top = q.top();
        q.pop();
        q.push(top);
    }
}

template <typename pri_queue,
          typename handle_container
         >
void fill_heap_with_handles(pri_queue & q, handle_container & handles, int index, int size, int offset = 0)
{
    test_data const & data = get_data(index);

    for (int i = 0; i != size + 1; ++i) {
        handles[i] = q.push(data[i]);

        if (i > 0) {
            typename pri_queue::handle_type last = handles[i-1];
            int val = *last;
            q.erase(last);
            handles[i-1] = q.push(val);
        }
    }
}


template <typename pri_queue>
struct run_sequential_push
{
    run_sequential_push(int size):
        size(size)
    {}

    void prepare(int index)
    {
        q.clear();
        fill_heap(q, index, size);
    }

    no_inline void operator()(int index)
    {
        test_data const & data = get_data(index);

        for (int i = 0; i != 16; ++i)
            q.push(data[(1<<max_data) + i]);
    }

    pri_queue q;
    int size;
};

DEFINE_BENCHMARKS_SELECTOR(sequential_push)

template <typename pri_queue>
struct run_sequential_pop
{
    run_sequential_pop(int size):
        size(size)
    {}

    void prepare(int index)
    {
        q.clear();
        fill_heap(q, index, size);
    }

    no_inline void operator()(int index)
    {
        for (int i = 0; i != 16; ++i)
            q.pop();
    }

    pri_queue q;
    int size;
};

DEFINE_BENCHMARKS_SELECTOR(sequential_pop)

template <typename pri_queue>
struct run_sequential_increase
{
    run_sequential_increase(int size):
        size(size), handles(size)
    {}

    void prepare(int index)
    {
        q.clear();
        fill_heap_with_handles(q, handles, index, size);
    }

    no_inline void operator()(int index)
    {
        test_data const & data = get_data(index);
        for (int i = 0; i != 16; ++i)
            q.increase(handles[i], data[i] + (1<<max_data));
    }

    pri_queue q;
    int size;
    std::vector<typename pri_queue::handle_type> handles;
};

DEFINE_BENCHMARKS_SELECTOR(sequential_increase)

template <typename pri_queue>
struct run_sequential_decrease
{
    run_sequential_decrease(int size):
        size(size), handles(size)
    {}

    void prepare(int index)
    {
        q.clear();
        fill_heap_with_handles(q, handles, index, size);
    }

    no_inline void operator()(int index)
    {
        test_data const & data = get_data(index);
        for (int i = 0; i != 16; ++i)
            q.decrease(handles[i], data[i] + (1<<max_data));
    }

    pri_queue q;
    int size;
    std::vector<typename pri_queue::handle_type> handles;
};

DEFINE_BENCHMARKS_SELECTOR(sequential_decrease)

template <typename pri_queue>
struct run_merge_and_clear
{
    run_merge_and_clear(int size):
        size(size)
    {}

    void prepare(int index)
    {
        q.clear();
        r.clear();

        fill_heap(q, index, size);
        fill_heap(r, index, size, size);
    }

    no_inline void operator()(int index)
    {
        boost::heap::heap_merge(q, r);
    }

    pri_queue q, r;
    int size;
};

DEFINE_BENCHMARKS_SELECTOR(merge_and_clear)

template <typename pri_queue>
struct run_equivalence
{
    run_equivalence(int size):
        size(size)
    {}

    void prepare(int index)
    {
        q.clear();
        r.clear();
        s.clear();

        fill_heap(q, index, size);
        fill_heap(r, index, size, size);
        fill_heap(s, index, size);
    }

    no_inline bool operator()(int index)
    {
        return (q == r) ^ (q == s);
    }

    pri_queue q, r, s;
    int size;
};

DEFINE_BENCHMARKS_SELECTOR(equivalence)


template <typename benchmark>
inline double run_benchmark(benchmark & b)
{
    boost::high_resolution_timer timer;
    std::vector<double> results;

    for (int i = 0; i != num_benchmarks; ++i)
    {
        b.prepare(i);
        timer.restart();
        b(i);
        double t = timer.elapsed();
        results.push_back(t);
    }

    return results.at(results.size()/2); // median
}