Repository navigation
Expand file tree
/
Copy pathht.cpp
More file actions
87 lines (68 loc) · 2.26 KB
/
Copy pathht.cpp
File metadata and controls
87 lines (68 loc) · 2.26 KB
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
#include <cmath>
#include <iostream>
#include <random>
#include <string>
#include <unordered_map>
#include <vector>
#include "colors.h"
#include "timer.h"
#ifdef HAVE_BOOST
#include <boost/container/small_vector.hpp>
#include <boost/pool/pool.hpp>
template <typename T>
class pool_alloc {
boost::pool<boost::default_user_allocator_malloc_free> pool_;
public:
using value_type = T;
pool_alloc() : pool_(sizeof(T)) { }
template <typename U> pool_alloc(const pool_alloc<U>&) : pool_alloc() { }
T *allocate(const size_t n) {
T* ret;
ret = static_cast<T*>( (n == 1) ? pool_.malloc() : std::malloc(n * sizeof(T)) );
if (!ret && n) throw std::bad_alloc();
return ret;
}
void deallocate(T* ptr, const size_t n) {
if (ptr && n)
if (n == 1) pool_.free(ptr); else std::free(ptr);
}
};
template <typename T, typename U> bool operator==(const pool_alloc<T>& a, const pool_alloc<U>& b) { return &a == &b; }
template <typename T, typename U> bool operator!=(const pool_alloc<T>& a, const pool_alloc<U>& b) { return &a != &b; }
#endif /* HAVE_BOOST */
void benchmark(size_t n)
{
#pragma omp parallel
{
std::random_device rd;
std::mt19937 gen(rd());
std::uniform_int_distribution<int> keydist;
std::gamma_distribution<> lendist(2.0);
#if defined(HAVE_BOOST) && defined(SBO)
using value_t = boost::container::small_vector<float, SBO>;
#else
using value_t = std::vector<float>;
#endif
#if defined(HAVE_BOOST) && defined(MP)
std::unordered_map<int, value_t, std::hash<int>, std::equal_to<int>, pool_alloc<std::pair<const int, value_t>>> um;
#else
std::unordered_map<key_t, value_t> um;
#endif
um.reserve(n);
for (size_t i = 0; i < n; i++) {
size_t len = 1 + static_cast<size_t>(lendist(gen));
auto p = um.emplace(std::make_pair(keydist(gen), value_t(len)));
}
}
}
int main()
{
static const size_t n = 10'000'000;
chrono_timer<> timer(chrono_timer<>::start_now);
benchmark(n);
timer.stop();
auto d = timer.seconds();
std::cout << cyan << "Absolute runtime = " << d << " [s]" << reset << std::endl;
auto rate = (double)n / d / 1'000'000;
std::cout << yellow << "Update rate per thread = " << rate << " [Mupdate/s]" << reset << std::endl;
}