FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
exprtk/exprtk_benchmark.cpp at master · ArashPartow/exprtk · GitHub
ArashPartow
/
exprtk
Public
Notifications
You must be signed in to change notification settings
Fork
322
Star
1k
Code
Pull requests
0
Actions
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Security and quality
Insights
Expand file tree
Breadcrumbs
exprtk
/
exprtk_benchmark.cpp
Copy path
More file actions
More file actions
Latest commit
History
History
History
567 lines (455 loc) · 16.3 KB
Breadcrumbs
exprtk
/
exprtk_benchmark.cpp
Copy path
File metadata and controls
567 lines (455 loc) · 16.3 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
/*
**************************************************************
* C++ Mathematical Expression Toolkit Library *
* *
* ExprTk vs Native Benchmarks *
* Author: Arash Partow (1999-2025) *
* 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
<
cmath
>
#
include
<
fstream
>
#
include
<
string
>
#
include
<
deque
>
#
include
"
exprtk.hpp
"
const
std::string global_expression_list[] =
{
"
(y + x)
"
,
"
2 * (y + x)
"
,
"
(2 * y + 2 * x)
"
,
"
((1.23 * x^2) / y) - 123.123
"
,
"
(y + x / y) * (x - y / x)
"
,
"
x / ((x + y) + (x - y)) / y
"
,
"
1 - ((x * y) + (y / x)) - 3
"
,
"
(5.5 + x) + (2 * x - 2 / 3 * y) * (x / 3 + y / 4) + (y + 7.7)
"
,
"
1.1x^1 + 2.2y^2 - 3.3x^3 + 4.4y^15 - 5.5x^23 + 6.6y^55
"
,
"
sin(2 * x) + cos(pi / y)
"
,
"
1 - sin(2 * x) + cos(pi / y)
"
,
"
sqrt(111.111 - sin(2 * x) + cos(pi / y) / 333.333)
"
,
"
(x^2 / sin(2 * pi / y)) - x / 2
"
,
"
x + (cos(y - sin(2 / x * pi)) - sin(x - cos(2 * y / pi))) - y
"
,
"
clamp(-1.0, sin(2 * pi * x) + cos(y / 2 * pi), +1.0)
"
,
"
max(3.33, min(sqrt(1 - sin(2 * x) + cos(pi / y) / 3), 1.11))
"
,
"
if((y + (x * 2.2)) <= (x + y + 1.1), x - y, x * y) + 2 * pi / x
"
};
const
std::
size_t
global_expression_list_size =
sizeof
(global_expression_list) /
sizeof
(std::string);
static
const
double
global_lower_bound_x = -
100.0
;
static
const
double
global_lower_bound_y = -
100.0
;
static
const
double
global_upper_bound_x = +
100.0
;
static
const
double
global_upper_bound_y = +
100.0
;
static
const
double
global_delta =
0.0111
;
template
<
typename
T,
typename
Allocator,
template
<
typename
,
typename
>
class
Sequence
>
bool
load_expression
(exprtk::symbol_table<T>& symbol_table,
Sequence<exprtk::expression<T>,Allocator>& expr_seq)
{
exprtk::parser<
double
> parser;
for
(std::
size_t
i =
0
; i < global_expression_list_size; ++i)
{
exprtk::expression<
double
> expression;
expression.
register_symbol_table
(symbol_table);
if
(!parser.
compile
(global_expression_list[i],expression))
{
printf
(
"
[load_expression] - Parser Error: %s
\t
Expression: %s
\n
"
,
parser.
error
().
c_str
(),
global_expression_list[i].
c_str
());
return
false
;
}
expr_seq.
push_back
(expression);
}
return
true
;
}
template
<
typename
T>
void
run_exprtk_benchmark
(T& x, T& y,
exprtk::expression<T>& expression,
const
std::string& expr_string)
{
T total =
T
(
0
);
unsigned
int
count =
0
;
exprtk::timer timer;
timer.
start
();
for
(x = global_lower_bound_x; x <= global_upper_bound_x; x += global_delta)
{
for
(y = global_lower_bound_y; y <= global_upper_bound_y; y += global_delta)
{
total += expression.
value
();
++count;
}
}
timer.
stop
();
if
(
T
(
0
) != total)
printf
(
"
[exprtk] Total Time:%12.8f Rate:%14.3fevals/sec Expression: %s
\n
"
,
timer.
time
(),
count / timer.
time
(),
expr_string.
c_str
());
else
printf
(
"
run_exprtk_benchmark() - Error running benchmark for expression: %s
\n
"
,expr_string.
c_str
());
}
template
<
typename
T>
struct
native
;
template
<
typename
T,
typename
NativeFunction>
void
run_native_benchmark
(T& x, T& y, NativeFunction f,
const
std::string& expr_string)
{
T total =
T
(
0
);
unsigned
int
count =
0
;
exprtk::timer timer;
timer.
start
();
for
(x = global_lower_bound_x; x <= global_upper_bound_x; x += global_delta)
{
for
(y = global_lower_bound_y; y <= global_upper_bound_y; y += global_delta)
{
total +=
f
(x,y);
++count;
}
}
timer.
stop
();
if
(
T
(
0
) != total)
printf
(
"
[native] Total Time:%12.8f Rate:%14.3fevals/sec Expression: %s
\n
"
,
timer.
time
(),
count / timer.
time
(),
expr_string.
c_str
());
else
printf
(
"
run_native_benchmark() - Error running benchmark for expression: %s
\n
"
,expr_string.
c_str
());
}
template
<
typename
T>
bool
run_parse_benchmark
(exprtk::symbol_table<T>& symbol_table)
{
static
const
std::
size_t
rounds =
100000
;
exprtk::parser<
double
> parser;
exprtk::expression<
double
> expression;
expression.
register_symbol_table
(symbol_table);
for
(std::
size_t
i =
0
; i < global_expression_list_size; ++i)
{
exprtk::timer timer;
timer.
start
();
for
(std::
size_t
r =
0
; r < rounds; ++r)
{
if
(!parser.
compile
(global_expression_list[i],expression))
{
printf
(
"
[run_parse_benchmark] - Parser Error: %s
\t
Expression: %s
\n
"
,
parser.
error
().
c_str
(),
global_expression_list[i].
c_str
());
return
false
;
}
}
timer.
stop
();
printf
(
"
[parse] Total Time:%12.8f Rate:%14.3fparse/sec Expression: %s
\n
"
,
timer.
time
(),
rounds / timer.
time
(),
global_expression_list[i].
c_str
());
}
return
true
;
}
const
double
pi =
3.141592653589793238462643383279502
;
template
<
typename
T>
struct
native
{
typedef
typename
exprtk::details::
functor_t
<T>
functor_t
;
typedef
typename
functor_t
::Type Type;
static
inline
T
avg
(Type x, Type y)
{
return
(x + y) /
T
(
2
);
}
static
inline
T
clamp
(
const
Type l,
const
Type v,
const
Type u)
{
return
((v < l) ? l : ((v > u) ? u : v));
}
static
inline
T
func00
(Type x, Type y)
{
return
(y + x);
}
static
inline
T
func01
(Type x, Type y)
{
return
T
(
2
) * (y + x);
}
static
inline
T
func02
(Type x, Type y)
{
return
(
T
(
2
) * y +
T
(
2
) * x);
}
static
inline
T
func03
(Type x, Type y)
{
return
((
T
(
1.23
) * (x * x)) / y) -
T
(
123.123
);
}
static
inline
T
func04
(Type x, Type y)
{
return
(y + x / y) * (x - y / x);
}
static
inline
T
func05
(Type x, Type y)
{
return
x / ((x + y) + (x - y)) / y;
}
static
inline
T
func06
(Type x, Type y)
{
return
T
(
1
) - ((x * y) + (y / x)) -
T
(
3
);
}
static
inline
T
func07
(Type x, Type y)
{
return
(
T
(
5.5
) + x) + (
T
(
2
) * x -
T
(
2
) /
T
(
3
) * y) * (x /
T
(
3
) + y /
T
(
4
)) + (y +
T
(
7.7
));
}
static
inline
T
func08
(Type x, Type y)
{
using
namespace
std
;
return
(
T
(
1.1
)*
pow
(x,
T
(
1
))+
T
(
2.2
)*
pow
(y,
T
(
2
))-
T
(
3.3
)*
pow
(x,
T
(
3
))+
T
(
4.4
)*
pow
(y,
T
(
15
))-
T
(
5.5
)*
pow
(x,
T
(
23
))+
T
(
6.6
)*
pow
(y,
T
(
55
)));
}
static
inline
T
func09
(Type x, Type y)
{
return
std::sin
(
T
(
2
) * x) +
std::cos
(pi / y);
}
static
inline
T
func10
(Type x, Type y)
{
return
T
(
1
) -
std::sin
(
T
(
2
) * x) +
std::cos
(pi / y);
}
static
inline
T
func11
(Type x, Type y)
{
return
std::sqrt
(
T
(
111.111
) -
std::sin
(
T
(
2
) * x) +
std::cos
(pi / y) /
T
(
333.333
));
}
static
inline
T
func12
(Type x, Type y)
{
return
((x * x) /
std::sin
(
T
(
2
) * pi / y)) - x /
T
(
2
);
}
static
inline
T
func13
(Type x, Type y)
{
return
(x + (
std::cos
(y -
std::sin
(
T
(
2
) / x * pi)) -
std::sin
(x -
std::cos
(
T
(
2
) * y / pi))) - y);
}
static
inline
T
func14
(Type x, Type y)
{
return
clamp
(
T
(-
1
),
std::sin
(
T
(
2
) * pi * x) +
std::cos
(y /
T
(
2
) * pi), +
T
(
1
));
}
static
inline
T
func15
(Type x, Type y)
{
return
std::max
(
T
(
3.33
),
std::min
(
sqrt
(
T
(
1
) -
std::sin
(
T
(
2
) * x) +
std::cos
(pi / y) /
T
(
3
)),
T
(
1.11
)));
}
static
inline
T
func16
(Type x, Type y)
{
return
(((y + (x *
T
(
2.2
))) <= (x + y +
T
(
1.1
))) ? x - y : x * y) +
T
(
2
) * pi / x;
}
};
double
pgo_primer
();
void
perform_file_based_benchmark
(
const
std::string& file_name,
const
std::
size_t
& rounds =
100000
);
int
main
(
int
argc,
char
* argv[])
{
if
(argc >=
2
)
{
const
std::string file_name = argv[
1
];
if
(argc ==
2
)
perform_file_based_benchmark
(file_name);
else
perform_file_based_benchmark
(file_name,
atoi
(argv[
2
]));
return
0
;
}
pgo_primer
();
double
x =
0
;
double
y =
0
;
exprtk::symbol_table<
double
> symbol_table;
symbol_table.
add_constants
();
symbol_table.
add_variable
(
"
x
"
,x);
symbol_table.
add_variable
(
"
y
"
,y);
std::deque<exprtk::expression<
double
> > compiled_expr_list;
if
(!
load_expression
(symbol_table,compiled_expr_list))
{
return
1
;
}
{
printf
(
"
--- EXPRTK ---
\n
"
);
for
(std::
size_t
i =
0
; i < compiled_expr_list.
size
(); ++i)
{
run_exprtk_benchmark
(x,y,compiled_expr_list[i],global_expression_list[i]);
}
}
{
printf
(
"
--- NATIVE ---
\n
"
);
run_native_benchmark
(x, y, native<
double
>::func00,global_expression_list[
0
]);
run_native_benchmark
(x, y, native<
double
>::func01,global_expression_list[
1
]);
run_native_benchmark
(x, y, native<
double
>::func02,global_expression_list[
2
]);
run_native_benchmark
(x, y, native<
double
>::func03,global_expression_list[
3
]);
run_native_benchmark
(x, y, native<
double
>::func04,global_expression_list[
4
]);
run_native_benchmark
(x, y, native<
double
>::func05,global_expression_list[
5
]);
run_native_benchmark
(x, y, native<
double
>::func06,global_expression_list[
6
]);
run_native_benchmark
(x, y, native<
double
>::func07,global_expression_list[
7
]);
run_native_benchmark
(x, y, native<
double
>::func08,global_expression_list[
8
]);
run_native_benchmark
(x, y, native<
double
>::func09,global_expression_list[
9
]);
run_native_benchmark
(x, y, native<
double
>::func10,global_expression_list[
10
]);
run_native_benchmark
(x, y, native<
double
>::func11,global_expression_list[
11
]);
run_native_benchmark
(x, y, native<
double
>::func12,global_expression_list[
12
]);
run_native_benchmark
(x, y, native<
double
>::func13,global_expression_list[
13
]);
run_native_benchmark
(x, y, native<
double
>::func14,global_expression_list[
14
]);
run_native_benchmark
(x, y, native<
double
>::func15,global_expression_list[
15
]);
run_native_benchmark
(x, y, native<
double
>::func16,global_expression_list[
16
]);
}
{
printf
(
"
--- PARSE ----
\n
"
);
run_parse_benchmark
(symbol_table);
}
return
0
;
}
double
pgo_primer
()
{
static
const
double
lower_bound_x = -
50.0
;
static
const
double
lower_bound_y = -
50.0
;
static
const
double
upper_bound_x = +
50.0
;
static
const
double
upper_bound_y = +
50.0
;
static
const
double
delta =
0.07
;
double
total =
0.0
;
for
(
double
x = lower_bound_x; x <= upper_bound_x; x += delta)
{
for
(
double
y = lower_bound_y; y <= upper_bound_y; y += delta)
{
total += native<
double
>::
func00
(x,y);
total += native<
double
>::
func01
(x,y);
total += native<
double
>::
func02
(x,y);
total += native<
double
>::
func03
(x,y);
total += native<
double
>::
func04
(x,y);
total += native<
double
>::
func05
(x,y);
total += native<
double
>::
func06
(x,y);
total += native<
double
>::
func07
(x,y);
total += native<
double
>::
func08
(x,y);
total += native<
double
>::
func09
(x,y);
total += native<
double
>::
func10
(x,y);
total += native<
double
>::
func11
(x,y);
total += native<
double
>::
func12
(x,y);
total += native<
double
>::
func13
(x,y);
total += native<
double
>::
func14
(x,y);
total += native<
double
>::
func15
(x,y);
total += native<
double
>::
func16
(x,y);
}
}
return
total;
}
inline
std::
size_t
load_expression_file
(
const
std::string& file_name, std::deque<std::string>& expression_list)
{
std::ifstream
stream
(file_name.
c_str
());
if
(!stream)
return
0
;
std::string buffer;
buffer.
reserve
(
1024
);
while
(
std::getline
(stream,buffer))
{
if
(buffer.
empty
())
continue
;
else
if
(
'
#
'
== buffer[
0
])
continue
;
expression_list.
push_back
(buffer);
}
return
expression_list.
size
();
}
void
perform_file_based_benchmark
(
const
std::string& file_name,
const
std::
size_t
& rounds)
{
std::deque<std::string> expr_str_list;
if
(
0
==
load_expression_file
(file_name,expr_str_list))
{
printf
(
"
Failed to load any expressions from: %s
\n
"
, file_name.
c_str
());
return
;
}
typedef
exprtk::symbol_table<
double
>
symbol_table_t
;
typedef
exprtk::expression<
double
>
expression_t
;
typedef
exprtk::parser<
double
>
parser_t
;
std::deque<
expression_t
> expression_list;
symbol_table_t
symbol_table;
double
a =
1.1
;
double
b =
2.2
;
double
c =
3.3
;
double
x =
2.123456
;
double
y =
3.123456
;
double
z =
4.123456
;
double
w =
5.123456
;
exprtk::rtl::vecops::package<
double
> vector_package;
symbol_table.
add_variable
(
"
a
"
, a);
symbol_table.
add_variable
(
"
b
"
, b);
symbol_table.
add_variable
(
"
c
"
, c);
symbol_table.
add_variable
(
"
x
"
, x);
symbol_table.
add_variable
(
"
y
"
, y);
symbol_table.
add_variable
(
"
z
"
, z);
symbol_table.
add_variable
(
"
w
"
, w);
exprtk::polynomial<
double
,
1
> poly01;
exprtk::polynomial<
double
,
2
> poly02;
exprtk::polynomial<
double
,
3
> poly03;
exprtk::polynomial<
double
,
4
> poly04;
exprtk::polynomial<
double
,
5
> poly05;
exprtk::polynomial<
double
,
6
> poly06;
exprtk::polynomial<
double
,
7
> poly07;
exprtk::polynomial<
double
,
8
> poly08;
exprtk::polynomial<
double
,
9
> poly09;
exprtk::polynomial<
double
,
10
> poly10;
exprtk::polynomial<
double
,
11
> poly11;
exprtk::polynomial<
double
,
12
> poly12;
symbol_table.
add_function
(
"
poly01
"
, poly01);
symbol_table.
add_function
(
"
poly02
"
, poly02);
symbol_table.
add_function
(
"
poly03
"
, poly03);
symbol_table.
add_function
(
"
poly04
"
, poly04);
symbol_table.
add_function
(
"
poly05
"
, poly05);
symbol_table.
add_function
(
"
poly06
"
, poly06);
symbol_table.
add_function
(
"
poly07
"
, poly07);
symbol_table.
add_function
(
"
poly08
"
, poly08);
symbol_table.
add_function
(
"
poly09
"
, poly09);
symbol_table.
add_function
(
"
poly10
"
, poly10);
symbol_table.
add_function
(
"
poly11
"
, poly11);
symbol_table.
add_function
(
"
poly12
"
, poly12);
symbol_table.
add_package
(vector_package);
static
double
e = exprtk::details::numeric::constant::e;
symbol_table.
add_variable
(
"
e
"
, e,
true
);
symbol_table.
add_constants
();
{
parser_t
parser;
for
(std::
size_t
i =
0
; i < expr_str_list.
size
(); ++i)
{
expression_t
expression;
expression.
register_symbol_table
(symbol_table);
if
(!parser.
compile
(expr_str_list[i],expression))
{
printf
(
"
[perform_file_based_benchmark] - Parser Error: %s
\t
Expression: %s
\n
"
,
parser.
error
().
c_str
(),
expr_str_list[i].
c_str
());
return
;
}
expression_list.
push_back
(expression);
}
}
exprtk::timer total_timer;
exprtk::timer timer;
double
single_eval_total_time =
0.0
;
total_timer.
start
();
for
(std::
size_t
i =
0
; i < expression_list.
size
(); ++i)
{
expression_t
& expression = expression_list[i];
a =
1.1
;
b =
2.2
;
c =
3.3
;
x =
2.123456
;
y =
3.123456
;
z =
4.123456
;
w =
5.123456
;
timer.
start
();
double
sum =
0.0
;
for
(std::
size_t
r =
0
; r < rounds; ++r)
{
sum += expression.
value
();
std::swap
(a,b);
std::swap
(x,y);
}
timer.
stop
();
printf
(
"
Expression %3d of %3d %9.3f ns
\t
%10d ns
\t
(%30.10f) '%s'
\n
"
,
static_cast
<
int
>(i +
1
),
static_cast
<
int
>(expression_list.
size
()),
(timer.
time
() *
1000000000.0
) / (
1.0
* rounds),
static_cast
<
int
>(timer.
time
() *
1000000000.0
),
sum,
expr_str_list[i].
c_str
());
fflush
(stdout);
single_eval_total_time += (timer.
time
() *
1000000000.0
) / (
1.0
* rounds);
}
total_timer.
stop
();
printf
(
"
[*] Number Of Evals: %15.0f
\n
"
,
rounds * (expression_list.
size
() *
1.0
));
printf
(
"
[*] Total Time: %9.3fsec
\n
"
,
total_timer.
time
());
printf
(
"
[*] Total Single Eval Time: %9.3fms
\n
"
,
single_eval_total_time /
1000000.0
);
}
Back
|
FazBrowse Home
|
New Git URL