C++ Mathematical Expression Toolkit (ExprTk)
release
Toggle main menu visibility
Loading...
Searching...
No Matches
exprtk
exprtk_vector_benchmark.cpp
Go to the documentation of this file.
1
/*
2
**************************************************************
3
* C++ Mathematical Expression Toolkit Library *
4
* *
5
* ExprTk Vector Processing Benchmark *
6
* Author: Arash Partow (1999-2025) *
7
* URL: https://www.partow.net/programming/exprtk/index.html *
8
* *
9
* Copyright notice: *
10
* Free use of the Mathematical Expression Toolkit Library is *
11
* permitted under the guidelines and in accordance with the *
12
* most current version of the MIT License. *
13
* https://www.opensource.org/licenses/MIT *
14
* SPDX-License-Identifier: MIT *
15
* *
16
**************************************************************
17
*/
18
19
20
#include <cstdio>
21
#include <fstream>
22
#include <limits>
23
#include <string>
24
#include <vector>
25
26
#include "
exprtk.hpp
"
27
28
29
struct
test_expression
30
{
31
std::size_t
cost
;
32
std::string
e
;
33
34
test_expression
(
const
std::size_t c,
const
std::string& ex)
35
:
cost
(c)
36
,
e
(ex)
37
{}
38
};
39
40
const
test_expression
global_expression_list
[] =
41
{
42
test_expression
( 1,
"2 * v0"
),
43
test_expression
( 1,
"v0 * 2"
),
44
test_expression
( 2,
"2v0 + 3"
),
45
test_expression
( 2,
"3 + 2v0"
),
46
test_expression
( 5,
"(2v0 + 3) * (2v0 + 3)"
),
47
test_expression
( 5,
"(3 + 2v0) * (3 + 2v0)"
),
48
test_expression
( 1,
"v0 + v1"
),
49
test_expression
( 3,
"(v0 + v1) * (v0 - v1)"
),
50
test_expression
( 3,
"2v0 + 3v1"
),
51
test_expression
( 3,
"2v0 - v1 / 3"
),
52
test_expression
( 2,
"v0 * v1 / v2"
),
53
test_expression
( 3,
"2(v0 * v1 / v2)"
),
54
test_expression
( 4,
"2(v0 / 3 + v1 / 4)"
),
55
test_expression
( 3,
"(2v0 - v1 / v2)"
),
56
test_expression
( 4,
"3(2v0 - v1 / v2)"
),
57
test_expression
( 5,
"7(5v0 * 3v1 / 2v2)"
),
58
test_expression
( 5,
"abs(v0 - v1) * abs(v1 - v0)"
),
59
test_expression
( 7,
"abs(2v0 - v1) * abs(v1 - 2v0)"
),
60
test_expression
( 9,
"abs(2v0 - 3v1) * abs(3v1 - 5v0)"
),
61
test_expression
( 2,
"sum(2 * v0)"
),
62
test_expression
( 2,
"sum(v0 * 2)"
),
63
test_expression
( 2,
"sum(v0 * v1)"
),
64
test_expression
( 3,
"sum(2v0 + 3)"
),
65
test_expression
( 3,
"sum(3 + 2v0)"
),
66
test_expression
( 6,
"sum((2v0 + 3) * (2v0 + 3))"
),
67
test_expression
( 6,
"sum((3 + 2v0) * (3 + 2v0))"
),
68
test_expression
( 2,
"sum(v0 + v1)"
),
69
test_expression
( 4,
"sum((v0 + v1) * (v0 - v1))"
),
70
test_expression
( 4,
"sum(2v0 + 3v1)"
),
71
test_expression
( 4,
"sum((2v0 - v1 / v2))"
),
72
test_expression
( 5,
"sum(3(2v0 - v1 / v2))"
),
73
test_expression
( 4,
"sum(abs(v0 * v1) / v2)"
),
74
test_expression
( 5,
"sum(v0 + v1) + avg(v0 - v1)"
),
75
test_expression
( 8,
"7(sum(abs(5v0 * 3v1) / 2v2))"
),
76
test_expression
( 9,
"sum(2v0 + 3v1) + sum(4 / v0 - 5 / v1)"
),
77
test_expression
( 6,
"sum(abs(v0 - v1) * abs(v1 - v0))"
),
78
test_expression
( 8,
"sum(abs(2v0 - v1) * abs(v1 - 2v0))"
),
79
test_expression
(10,
"sum(abs(2v0 - 3v1) * abs(3v1 - 5v0))"
),
80
test_expression
( 2,
"axpbz(2,v0,3,v1)"
),
81
test_expression
( 2,
"axpy(2,v0,v1)"
),
82
//test_expression( 4, "sum(v0^2.2 + v1^3.3)" ),
83
//test_expression( 4, "exp(-1 / (v0^2))" ),
84
//test_expression( 5, "exp(-1 / (v0^2)) / v1" )
85
};
86
87
const
std::size_t
global_expression_list_size
=
sizeof
(
global_expression_list
) /
sizeof
(
test_expression
);
88
89
const
std::size_t
rounds
= 2000;
90
91
typedef
double
numeric_type
;
92
93
template
<
typename
T>
94
T
run_expression_benchmark
(
const
std::size_t vec_size,
95
const
std::string& expr_string,
96
const
std::size_t& cost)
97
{
98
typedef
exprtk::symbol_table<T>
symbol_table_t;
99
typedef
exprtk::expression<T>
expression_t;
100
typedef
exprtk::parser<T>
parser_t;
101
102
std::vector<T> v0(vec_size, T(2.1234567890));
103
std::vector<T> v1(vec_size, T(3.1234567890));
104
std::vector<T> v2(vec_size, T(5.1234567890));
105
106
exprtk::rtl::vecops::package<T>
vecops_package;
107
108
symbol_table_t symbol_table;
109
symbol_table.add_vector(
"v0"
,v0);
110
symbol_table.add_vector(
"v1"
,v1);
111
symbol_table.add_vector(
"v2"
,v2);
112
symbol_table.add_package(vecops_package);
113
114
expression_t expression;
115
expression.register_symbol_table(symbol_table);
116
117
parser_t parser;
118
119
if
(!parser.compile(expr_string,expression))
120
{
121
printf(
"[load_expression] - Parser Error: %s\tExpression: %s\n"
,
122
parser.error().c_str(),
123
expr_string.c_str());
124
125
return
std::numeric_limits<T>::quiet_NaN();
126
}
127
128
exprtk::timer
timer;
129
130
T total = T(0);
131
132
timer.
start
();
133
134
for
(std::size_t r = 0; r <
rounds
; ++r)
135
{
136
total += expression.value();
137
}
138
139
timer.
stop
();
140
141
if
(T(0) != total)
142
printf(
"Total Time:%10.7f Rate:%11.3fevals/sec Perf: %5.3fGFLOPS Expression: %s\n"
,
143
timer.
time
(),
144
rounds
/ timer.
time
(),
145
(
rounds
* vec_size * cost) / (1e+9 * timer.
time
()),
146
expr_string.c_str());
147
else
148
printf(
"run_expression_benchmark() - Error running benchmark for expression: %s\n"
,
149
expr_string.c_str());
150
151
return
timer.
time
();
152
}
153
154
template
<
typename
T>
155
void
run_benchmark
(
const
std::size_t& vec_size)
156
{
157
T total_time = 0.0;
158
159
for
(std::size_t i = 0; i <
global_expression_list_size
; ++i)
160
{
161
total_time +=
162
run_expression_benchmark<T>
(vec_size,
163
global_expression_list
[i].e,
164
global_expression_list
[i].cost);
165
}
166
167
unsigned
long
long
total_flops = 0;
168
169
for
(std::size_t i = 0; i <
global_expression_list_size
; ++i)
170
{
171
total_flops +=
global_expression_list
[i].cost;
172
}
173
174
total_flops *= (
rounds
* vec_size);
175
176
printf(
"Total Time:%10.7f FLOP: %llu Perf: %7.4fGFLOPS\n"
,
177
total_time,
178
total_flops,
179
total_flops / (1e9 * total_time));
180
}
181
182
template
<
typename
T>
183
void
run_file_benchmark
(
const
std::size_t& vec_size,
const
std::string& file_name)
184
{
185
std::ifstream stream(file_name.c_str());
186
187
if
(!stream)
188
{
189
printf(
"run_file_benchmark() - Failed to open file: %s\n"
,
190
file_name.c_str());
191
return
;
192
}
193
194
std::string line;
195
std::vector<std::string> expr_list;
196
197
while
(std::getline(stream, line))
198
{
199
if
(line.empty())
200
continue
;
201
else
if
(
'#'
== line[0])
202
continue
;
203
204
expr_list.push_back(line);
205
}
206
207
T total_time = T(0);
208
209
for
(std::size_t i = 0; i < expr_list.size(); ++i)
210
{
211
total_time +=
run_expression_benchmark<T>
(vec_size,expr_list[i],0);
212
}
213
214
printf(
"Total Time:%10.7f\n"
, total_time);
215
}
216
217
int
main
(
int
argc,
char
* argv[])
218
{
219
const
std::size_t vec_size = ((argc >= 2) ? atoi(argv[1]) : 100000);
220
const
std::string file_name = ((argc > 3) ? argv[2] :
""
);
221
222
switch
(argc)
223
{
224
case
2 :
run_benchmark <numeric_type>
(vec_size );
break
;
225
case
3 :
run_file_benchmark<numeric_type>
(vec_size,file_name);
break
;
226
}
227
228
return
0;
229
}
exprtk::expression
Definition
exprtk.hpp:21832
exprtk::parser
Definition
exprtk.hpp:22525
exprtk::symbol_table
Definition
exprtk.hpp:20090
exprtk::timer
Definition
exprtk.hpp:44056
exprtk::timer::stop
void stop()
Definition
exprtk.hpp:44103
exprtk::timer::time
double time() const
Definition
exprtk.hpp:44125
exprtk::timer::start
void start()
Definition
exprtk.hpp:44097
exprtk.hpp
main
int main()
Definition
exprtk_american_option_binomial_model.cpp:143
global_expression_list
const std::string global_expression_list[]
Definition
exprtk_benchmark.cpp:29
global_expression_list_size
const std::size_t global_expression_list_size
Definition
exprtk_benchmark.cpp:50
rounds
static const std::size_t rounds
Definition
exprtk_bsm_benchmark.cpp:26
numeric_type
mpfr::mpreal numeric_type
Definition
exprtk_mpfr_benchmark.cpp:32
test_expression
bool test_expression(const std::string &expression_string, const T &expected_result)
Definition
exprtk_mpfr_fiddle.cpp:41
run_file_benchmark
void run_file_benchmark(const std::size_t &vec_size, const std::string &file_name)
Definition
exprtk_vector_benchmark.cpp:183
run_benchmark
void run_benchmark(const std::size_t &vec_size)
Definition
exprtk_vector_benchmark.cpp:155
run_expression_benchmark
T run_expression_benchmark(const std::size_t vec_size, const std::string &expr_string, const std::size_t &cost)
Definition
exprtk_vector_benchmark.cpp:94
exprtk::rtl::vecops::package
Definition
exprtk.hpp:46626
test_expression
Definition
exprtk_vector_benchmark.cpp:30
test_expression::cost
std::size_t cost
Definition
exprtk_vector_benchmark.cpp:31
test_expression::e
std::string e
Definition
exprtk_vector_benchmark.cpp:32
test_expression::test_expression
test_expression(const std::size_t c, const std::string &ex)
Definition
exprtk_vector_benchmark.cpp:34
Generated by
1.17.0