]>
Commit | Line | Data |
---|---|---|
92f5a8d4 TL |
1 | // Copyright (C) 2016-2018 T. Zachary Laine |
2 | // | |
3 | // Distributed under the Boost Software License, Version 1.0. (See | |
4 | // accompanying file LICENSE_1_0.txt or copy at | |
5 | // http://www.boost.org/LICENSE_1_0.txt) | |
6 | #include <boost/yap/expression.hpp> | |
7 | ||
8 | #include <chrono> | |
9 | #include <iostream> | |
10 | ||
11 | #include <benchmark/benchmark.h> | |
12 | ||
13 | ||
14 | template<typename T> | |
15 | using term = boost::yap::terminal<boost::yap::expression, T>; | |
16 | ||
17 | namespace yap = boost::yap; | |
18 | namespace bh = boost::hana; | |
19 | ||
20 | ||
21 | //[ arithmetic_perf_decls | |
22 | namespace user { | |
23 | ||
24 | struct number | |
25 | { | |
26 | double value; | |
27 | ||
28 | friend number operator+(number lhs, number rhs) | |
29 | { | |
30 | return number{lhs.value + rhs.value}; | |
31 | } | |
32 | ||
33 | friend number operator*(number lhs, number rhs) | |
34 | { | |
35 | return number{lhs.value * rhs.value}; | |
36 | } | |
37 | }; | |
38 | } | |
39 | //] | |
40 | ||
41 | double get_noise() | |
42 | { | |
43 | auto const start_time = std::chrono::high_resolution_clock::now(); | |
44 | auto const start_time_ns = | |
45 | std::chrono::time_point_cast<std::chrono::nanoseconds>(start_time); | |
46 | return 1.0 * start_time_ns.time_since_epoch().count(); | |
47 | } | |
48 | ||
49 | ||
50 | user::number g_a{get_noise()}; | |
51 | user::number g_x{get_noise()}; | |
52 | user::number g_y{get_noise()}; | |
53 | ||
54 | //[ arithmetic_perf_eval_as_yap_expr | |
55 | user::number eval_as_yap_expr(user::number a_, user::number x_, user::number y_) | |
56 | { | |
57 | term<user::number> a{{a_}}; | |
58 | term<user::number> x{{x_}}; | |
59 | term<user::number> y{{y_}}; | |
60 | auto expr = (a * x + y) * (a * x + y) + (a * x + y); | |
61 | return yap::evaluate(expr); | |
62 | } | |
63 | //] | |
64 | ||
65 | void BM_eval_as_yap_expr(benchmark::State & state) | |
66 | { | |
67 | double d = 0; | |
68 | while (state.KeepRunning()) { | |
69 | user::number const n = eval_as_yap_expr(g_a, g_x, g_y); | |
70 | d += n.value; | |
71 | } | |
72 | std::cout << "Sum of doubles=" << d << "\n"; | |
73 | } | |
74 | ||
75 | //[ arithmetic_perf_eval_as_yap_expr_4x | |
76 | user::number | |
77 | eval_as_yap_expr_4x(user::number a_, user::number x_, user::number y_) | |
78 | { | |
79 | term<user::number> a{{a_}}; | |
80 | term<user::number> x{{x_}}; | |
81 | term<user::number> y{{y_}}; | |
82 | auto expr = (a * x + y) * (a * x + y) + (a * x + y) + | |
83 | (a * x + y) * (a * x + y) + (a * x + y) + | |
84 | (a * x + y) * (a * x + y) + (a * x + y) + | |
85 | (a * x + y) * (a * x + y) + (a * x + y); | |
86 | return yap::evaluate(expr); | |
87 | } | |
88 | //] | |
89 | ||
90 | void BM_eval_as_yap_expr_4x(benchmark::State & state) | |
91 | { | |
92 | double d = 0; | |
93 | while (state.KeepRunning()) { | |
94 | user::number const n = eval_as_yap_expr_4x(g_a, g_x, g_y); | |
95 | d += n.value; | |
96 | } | |
97 | std::cout << "Sum of doubles=" << d << "\n"; | |
98 | } | |
99 | ||
100 | //[ arithmetic_perf_eval_as_cpp_expr | |
101 | user::number eval_as_cpp_expr(user::number a, user::number x, user::number y) | |
102 | { | |
103 | return (a * x + y) * (a * x + y) + (a * x + y); | |
104 | } | |
105 | //] | |
106 | ||
107 | void BM_eval_as_cpp_expr(benchmark::State & state) | |
108 | { | |
109 | double d = 0; | |
110 | while (state.KeepRunning()) { | |
111 | user::number const n = eval_as_cpp_expr(g_a, g_x, g_y); | |
112 | d += n.value; | |
113 | } | |
114 | std::cout << "Sum of doubles=" << d << "\n"; | |
115 | } | |
116 | ||
117 | //[ arithmetic_perf_eval_as_cpp_expr_4x | |
118 | user::number eval_as_cpp_expr_4x(user::number a, user::number x, user::number y) | |
119 | { | |
120 | return (a * x + y) * (a * x + y) + (a * x + y) + (a * x + y) * (a * x + y) + | |
121 | (a * x + y) + (a * x + y) * (a * x + y) + (a * x + y) + | |
122 | (a * x + y) * (a * x + y) + (a * x + y); | |
123 | } | |
124 | //] | |
125 | ||
126 | void BM_eval_as_cpp_expr_4x(benchmark::State & state) | |
127 | { | |
128 | double d = 0; | |
129 | while (state.KeepRunning()) { | |
130 | user::number const n = eval_as_cpp_expr_4x(g_a, g_x, g_y); | |
131 | d += n.value; | |
132 | } | |
133 | std::cout << "Sum of doubles=" << d << "\n"; | |
134 | } | |
135 | ||
136 | BENCHMARK(BM_eval_as_yap_expr); | |
137 | BENCHMARK(BM_eval_as_yap_expr_4x); | |
138 | BENCHMARK(BM_eval_as_cpp_expr); | |
139 | BENCHMARK(BM_eval_as_cpp_expr_4x); | |
140 | ||
20effc67 | 141 | BENCHMARK_MAIN(); |