-
Notifications
You must be signed in to change notification settings - Fork 11
/
exprtk_funcall_benchmark.cpp
201 lines (154 loc) · 4.69 KB
/
exprtk_funcall_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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
/*
**************************************************************
* C++ Mathematical Expression Toolkit Library *
* *
* Function Call Benchmark (Custom vs Composite vs Native) *
* Author: Arash Partow (1999-2024) *
* URL: https://www.partow.net/programming/exprtk/index.html *
* *
* Copyright notice: *
* Free use of the Mathematical Expression Toolkit Library is *
* permitted under the guidelines and in accordance with the *
* most current version of the MIT License. *
* https://www.opensource.org/licenses/MIT *
* SPDX-License-Identifier: MIT *
* *
**************************************************************
*/
#include <cstdio>
#include <string>
#include "exprtk.hpp"
template <typename T>
struct func0 : public exprtk::ifunction<T>
{
using exprtk::ifunction<T>::operator();
func0()
: exprtk::ifunction<T>(2)
{}
inline T operator()(const T& v1, const T& v2)
{
return T(1) + std::cos(v1 * v2) / T(3);
}
};
template <typename T>
inline T func1(T v1, T v2)
{
return T(1) + std::cos(v1 * v2) / T(3);
}
template <typename T>
void function_call_benchmark01()
{
typedef exprtk::symbol_table<T> symbol_table_t;
typedef exprtk::expression<T> expression_t;
typedef exprtk::parser<T> parser_t;
typedef exprtk::function_compositor<T> compositor_t;
typedef typename compositor_t::function function_t;
T v1 = T(1);
T v2 = T(2);
func0<T> f0;
symbol_table_t symbol_table;
symbol_table.add_constants();
symbol_table.add_variable("v1" , v1 );
symbol_table.add_variable("v2" , v2 );
symbol_table.add_function("func0", f0 );
symbol_table.add_function("func1", func1<double>);
compositor_t compositor(symbol_table);
compositor.add(
function_t("func2")
.var("v1").var("v2")
.expression(" 1 + cos(v1 * v2) / 3;"));
const std::string program0 = "func0(v1,v2);";
const std::string program1 = "func1(v1,v2);";
const std::string program2 = "func2(v1,v2);";
expression_t expression0(symbol_table);
expression_t expression1(symbol_table);
expression_t expression2(symbol_table);
parser_t parser;
if (!parser.compile(program0,expression0))
{
printf("Error: %s\tExpression: %s\n",
parser.error().c_str(),
program0.c_str());
return;
}
if (!parser.compile(program1,expression1))
{
printf("Error: %s\tExpression: %s\n",
parser.error().c_str(),
program1.c_str());
return;
}
if (!parser.compile(program2,expression2))
{
printf("Error: %s\tExpression: %s\n",
parser.error().c_str(),
program1.c_str());
return;
}
const std::size_t rounds = 100000000;
{
T total = T(0);
exprtk::timer timer;
timer.start();
for (std::size_t r = 0; r < rounds; ++r)
{
total += expression0.value();
std::swap(v1,v2);
}
timer.stop();
printf("[custom function] Total time: %8.3fsec\tRate: %15.3f\tTotal:%20.5f\n",
timer.time(),
rounds / timer.time(),
total);
}
{
T total = T(0);
exprtk::timer timer;
timer.start();
for (std::size_t r = 0; r < rounds; ++r)
{
total += expression1.value();
std::swap(v1,v2);
}
timer.stop();
printf("[free function ] Total time: %8.3fsec\tRate: %15.3f\tTotal:%20.5f\n",
timer.time(),
rounds / timer.time(),
total);
}
{
T total = T(0);
exprtk::timer timer;
timer.start();
for (std::size_t r = 0; r < rounds; ++r)
{
total += expression2.value();
std::swap(v1,v2);
}
timer.stop();
printf("[compositor ] Total time: %8.3fsec\tRate: %15.3f\tTotal:%20.5f\n",
timer.time(),
rounds / timer.time(),
total);
}
{
T total = T(0);
exprtk::timer timer;
timer.start();
for (std::size_t r = 0; r < rounds; ++r)
{
total += func1<T>(v1,v2);
std::swap(v1,v2);
}
timer.stop();
printf("[native ] Total time: %8.3fsec\tRate: %15.3f\tTotal:%20.5f\n",
timer.time(),
rounds / timer.time(),
total);
}
}
int main()
{
function_call_benchmark01<double>();
return 0;
}