GCC Code Coverage Report


Directory: ./
File: benchmarks/bench_hash.cpp
Date: 2023-04-27 00:55:30
Exec Total Coverage
Lines: 0 31 0.0%
Functions: 0 12 0.0%
Branches: 0 68 0.0%

Line Branch Exec Source
1
2 #include "bench.h"
3
4 #include <iostream>
5 #include <random>
6
7 #define XXH_VECTOR XXH_AVX2
8
9 // only from Zen 4
10 // #define XXH_VECTOR XXH_AVX512
11
12 #include "xxh3.h"
13 #include "xxhash.c"
14 #include "xxhash.h"
15
16 using namespace lython;
17
18 std::size_t old_hash(String const& k) noexcept {
19 auto a = std::hash<std::string>();
20 return a(std::string(k.c_str()));
21 }
22
23 #if __linux__
24 std::size_t new_hash(String const& k) noexcept {
25 return std::_Hash_impl::hash(k.data(), k.length());
26 }
27 #else
28 std::size_t new_hash(String const& k) noexcept {
29 return std::_Hash_array_representation(k.c_str(), k.size());
30 }
31 #endif
32 std::size_t xx_hash_32(String const& k) noexcept { return XXH32(k.data(), k.length(), 0); }
33 std::size_t xx_hash_64(String const& k) noexcept { return XXH64(k.data(), k.length(), 0); }
34 std::size_t xx_hash_3(String const& k) noexcept { return XXH3_64bits(k.data(), k.length()); }
35
36 // check https://github.com/google/benchmark
37
38 lython::String make_string(size_t size = 64) {
39 static String str(size, ' ');
40 static std::random_device dev;
41 static std::mt19937 rng(dev());
42 static std::uniform_int_distribution<std::mt19937::result_type> uniform(0, 256);
43
44 str.resize(size);
45
46 for (int i = 0; i < size; i++) {
47 str[i] = uniform(rng);
48 }
49
50 return str;
51 }
52
53 int main() {
54 lython::String string = "owjfopwejfpwejfopwejfpwoejfpwef";
55
56 // but we need to check for collision too
57
58 make_string(64);
59
60 // clang-format off
61 auto comp = lython::Compare<int>({
62 lython::Benchmark<int>("OLD HASH", [](int size) {
63 //
64 lython::fakeuse(old_hash(make_string(size)));
65 }),
66 lython::Benchmark<int>("NEW HASH", [](int size) {
67 //
68 lython::fakeuse(new_hash(make_string(size)));
69 }),
70 lython::Benchmark<int>("XX HASH 32", [](int size) {
71 //
72 lython::fakeuse(xx_hash_32(make_string(size)));
73 }),
74 lython::Benchmark<int>("XX HASH 64", [](int size) {
75 //
76 lython::fakeuse(xx_hash_64(make_string(size)));
77 }),
78 lython::Benchmark<int>("XX HASH 3", [](int size) {
79 //
80 lython::fakeuse(xx_hash_3(make_string(size)));
81 })
82 }, 100, 10000);
83 // clang-format on
84
85 comp.add_setup(8);
86 comp.add_setup(16);
87 comp.add_setup(32);
88 comp.add_setup(64);
89
90 comp.run(std::cout);
91 comp.report(std::cout);
92
93 return 0;
94 }
95