-
Notifications
You must be signed in to change notification settings - Fork 122
/
benchmark.cpp
61 lines (55 loc) · 1.65 KB
/
benchmark.cpp
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
//
// Copyright (c) 2019-2020 Kris Jusiak (kris at jusiak dot net)
//
// 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)
//
#include <boost/ut.hpp>
#include <chrono>
#include <iostream>
#include <string>
#include <string_view>
namespace benchmark {
struct benchmark : boost::ut::detail::test {
explicit benchmark(std::string_view _name)
: boost::ut::detail::test{"benchmark", _name} {}
template <class Test>
auto operator=(Test _test) {
static_cast<boost::ut::detail::test&>(*this) = [&_test, this] {
const auto start = std::chrono::high_resolution_clock::now();
_test();
const auto stop = std::chrono::high_resolution_clock::now();
const auto ns =
std::chrono::duration_cast<std::chrono::nanoseconds>(stop - start);
std::clog << "[" << name << "] " << ns.count() << " ns\n";
};
}
};
[[nodiscard]] auto operator""_benchmark(const char* _name,
decltype(sizeof("")) size) {
return ::benchmark::benchmark{{_name, size}};
}
#if defined(__GNUC__) or defined(__clang__)
template <class T>
void do_not_optimize(T&& t) {
asm volatile("" ::"m"(t) : "memory");
}
#else
#pragma optimize("", off)
template <class T>
void do_not_optimize(T&& t) {
reinterpret_cast<char volatile&>(t) =
reinterpret_cast<char const volatile&>(t);
}
#pragma optimize("", on)
#endif
} // namespace benchmark
int main() {
using namespace boost::ut;
using namespace benchmark;
"string creation"_benchmark = [] {
std::string created_string{"hello"};
do_not_optimize(created_string);
};
}