BPHash
General object hashing library for C++
test_benchmark.cpp
Go to the documentation of this file.
1 /*! \file
2  * \brief Benchmarking of hashing algorithms
3  */
4 
5 /* Copyright (c) 2016 Benjamin Pritchard <ben@bennyp.org>
6  * This file is part of the BPHash project, which is released
7  * under the BSD 3-clause license. See the LICENSE file for details
8  */
9 
10 /* This file tests the core MurmurHash algorithms against
11  * the reference implementations in smhasher */
12 
13 #include <iostream>
14 #include <random>
15 #include <chrono>
16 #include <cstdlib>
17 
18 #include "bphash/Hasher.hpp"
19 #include "bphash/types/vector.hpp"
20 
25 
26 using namespace bphash;
27 using namespace std::chrono;
28 
29 
30 static void random_fill(std::vector<uint8_t> & buffer)
31 {
32  auto seed = system_clock::now().time_since_epoch().count();
33  std::default_random_engine generator(seed);
34  std::uniform_int_distribution<uint8_t> dist;
35 
36  for(auto & it : buffer)
37  it = dist(generator);
38 }
39 
40 
41 int main(int argc, char ** argv)
42 {
43  if(argc != 2)
44  {
45  std::cout << "\n Missing number of bytes to test\n";
46  std::cout << "\n usage: bphash_benchmark nbytes\n\n";
47  return 1;
48  }
49 
50  size_t nbytes = static_cast<size_t>(atol(argv[1]));
51 
52  std::vector<uint8_t> testdata(nbytes);
53  random_fill(testdata);
54 
55  const void * testdata_ptr = testdata.data();
56  const size_t testdata_size = testdata.size();
57 
58  std::cout << "\nTesting hashing of " << nbytes << " bytes\n";
59  std::cout << "Times in microseconds\n";
60 
61  high_resolution_clock timer_clock;
62 
63  // converts bytes/microsecond to GiB/second
64  const double conv_fac = 1.0e6/(1024.0 * 1024.0 * 1024.0);
65 
66  {
67  auto time0 = timer_clock.now();
69  mh32.update(testdata_ptr, testdata_size);
70  HashValue bph_32 = mh32.finalize();
71  auto time1 = timer_clock.now();
72  auto elapsed = duration_cast<microseconds>(time1-time0).count();
73  double rate = static_cast<double>(nbytes)/static_cast<double>(elapsed);
74  std::cout << " 32-bit x32 hash: " << elapsed
75  << " ( " << conv_fac*rate << " GiB/sec)\n";
76  }
77 
78  {
79  auto time0 = timer_clock.now();
81  mh32.update(testdata_ptr, testdata_size);
82  HashValue bph_32 = mh32.finalize();
83  auto time1 = timer_clock.now();
84  auto elapsed = duration_cast<microseconds>(time1-time0).count();
85  double rate = static_cast<double>(nbytes)/static_cast<double>(elapsed);
86  std::cout << " 32-bit x64 hash: " << elapsed
87  << " ( " << conv_fac*rate << " GiB/sec)\n";
88  }
89 
90  {
91  auto time0 = timer_clock.now();
93  mh64.update(testdata_ptr, testdata_size);
94  HashValue bph_64 = mh64.finalize();
95  auto time1 = timer_clock.now();
96  auto elapsed = duration_cast<microseconds>(time1-time0).count();
97  double rate = static_cast<double>(nbytes)/static_cast<double>(elapsed);
98  std::cout << " 64-bit x64 hash: " << elapsed
99  << " ( " << conv_fac*rate << " GiB/sec)\n";
100  }
101 
102  {
103  auto time0 = timer_clock.now();
105  mh128.update(testdata_ptr, testdata_size);
106  HashValue bph_128 = mh128.finalize();
107  auto time1 = timer_clock.now();
108  auto elapsed = duration_cast<microseconds>(time1-time0).count();
109  double rate = static_cast<double>(nbytes)/static_cast<double>(elapsed);
110  std::cout << " 128-bit x64 hash: " << elapsed
111  << " ( " << conv_fac*rate << " GiB/sec)\n";
112  }
113 
114  std::cout << "\nHashing of vector via make_hash\n";
115 
116  {
117  auto time0 = timer_clock.now();
118  HashValue bph_32 = make_hash(HashType::Hash32_x32, testdata);
119  auto time1 = timer_clock.now();
120  auto elapsed = duration_cast<microseconds>(time1-time0).count();
121  double rate = static_cast<double>(nbytes)/static_cast<double>(elapsed);
122  std::cout << " 32-bit x32 hash: " << elapsed
123  << " ( " << conv_fac*rate << " GiB/sec)\n";
124  }
125 
126  {
127  auto time0 = timer_clock.now();
128  HashValue bph_32 = make_hash(HashType::Hash32_x64, testdata);
129  auto time1 = timer_clock.now();
130  auto elapsed = duration_cast<microseconds>(time1-time0).count();
131  double rate = static_cast<double>(nbytes)/static_cast<double>(elapsed);
132  std::cout << " 32-bit x64 hash: " << elapsed
133  << " ( " << conv_fac*rate << " GiB/sec)\n";
134  }
135 
136  {
137  auto time0 = timer_clock.now();
138  HashValue bph_64 = make_hash(HashType::Hash64_x64, testdata);
139  auto time1 = timer_clock.now();
140  auto elapsed = duration_cast<microseconds>(time1-time0).count();
141  double rate = static_cast<double>(nbytes)/static_cast<double>(elapsed);
142  std::cout << " 64-bit x64 hash: " << elapsed
143  << " ( " << conv_fac*rate << " GiB/sec)\n";
144  }
145 
146  {
147  auto time0 = timer_clock.now();
148  HashValue bph_128 = make_hash(HashType::Hash128_x64, testdata);
149  auto time1 = timer_clock.now();
150  auto elapsed = duration_cast<microseconds>(time1-time0).count();
151  double rate = static_cast<double>(nbytes)/static_cast<double>(elapsed);
152  std::cout << " 128-bit x64 hash: " << elapsed
153  << " ( " << conv_fac*rate << " GiB/sec)\n";
154  }
155 
156  std::cout << "\n\n";
157 
158 
159 
160 
161  return 0;
162 }
163 
Implementation of MurmurHash3 32-bit x64 hash.
virtual void update(void const *data, size_t nbytes)
Add some data to the hash.
Default 32-bit hash for x86-64.
int main(int argc, char **argv)
Implementation of MurmurHash3 64-bit x64 hash.
Default 64-bit hash for x86-64.
Default 32-bit hash for x32.
Default 128-bit hash for x86-64.
A class that hashes objects (header)
MurmurHash3 32-bit x32 hash (header)
virtual HashValue finalize(void)
Finish hashing and report the hash.
static void random_fill(std::vector< uint8_t > &buffer)
HashValue make_hash(HashType type, const Targs &...objs)
Convenience function for hashing objects in a single function call.
Definition: Hasher.hpp:254
std::vector< uint8_t > HashValue
Stores the value of a hash.
Definition: Hash.hpp:24
MurmurHash3 128-bit x64 hash (header)
virtual HashValue finalize(void)
Finish hashing and report the hash.
MurmurHash3 32-bit x64 hash (header)
Hashing of std::vector.
virtual HashValue finalize(void)
Finish hashing and report the hash.
Implementation of MurmurHash3 32-bit x32 hash.
Implementation of MurmurHash3 128-bit x64 hash.
virtual void update(void const *data, size_t nbytes)
Add some data to the hash.
MurmurHash3 64-bit x64 hash (header)
virtual HashValue finalize(void)
Finish hashing and report the hash.