-
Notifications
You must be signed in to change notification settings - Fork 5
/
bench.cc
58 lines (50 loc) · 1.77 KB
/
bench.cc
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
// -*- mode: c++; coding: utf-8 -*-
// ra-ra/test - Test the benchmarking library.
// (c) Daniel Llorens - 2017
// This library is free software; you can redistribute it and/or modify it under
// the terms of the GNU Lesser General Public License as published by the Free
// Software Foundation; either version 3 of the License, or (at your option) any
// later version.
#include <string>
#include <limits>
#include <chrono>
#include <thread>
#include <iomanip>
#include <iostream>
#include "ra/test.hh"
using std::cout, std::endl, ra::TestRecorder, ra::Benchmark;
int main()
{
TestRecorder tr;
tr.section("straight");
{
auto f = [](auto && a, auto && b)
{
std::this_thread::sleep_for(std::chrono::nanoseconds(1));
return a+b;
};
auto b = Benchmark {/* repeats */ 100, /* runs */ 10};
auto vala = b.run(f, 1, 2);
cout << "empty: " << (ra::size(vala)==0) << endl;
b.report(std::cout, vala, 1e-9);
auto valb = b.run(f, ra::iota(3), ra::iota(10, 3));
b.report(std::cout, valb, 1e-9);
}
tr.section("fixture");
{
auto g = [](auto && repeat, auto && a, auto && b)
{
/* do stuff */
repeat([&]() { std::this_thread::sleep_for(std::chrono::nanoseconds(1));
return a+b; });
/* do stuff */
};
auto b = Benchmark {/* repeats */ 100, /* runs */ 10};
auto vala = b.run_f(g, 1, 2);
cout << "empty: " << (ra::size(vala)==0) << endl;
b.report(std::cout, vala, 1e-9);
auto valb = b.run_f(g, ra::iota(3), ra::iota(10, 3));
b.report(std::cout, valb, 1e-9);
}
return tr.summary();
};