C++ Mathematical Expression Toolkit (ExprTk) release
Loading...
Searching...
No Matches
Classes | Functions | Variables
exprtk_real_benchmark.cpp File Reference
#include <cstdio>
#include <cmath>
#include <iostream>
#include <fstream>
#include <string>
#include <deque>
#include "real_type.hpp"
#include "exprtk_real_adaptor.hpp"
#include "exprtk.hpp"
Include dependency graph for exprtk_real_benchmark.cpp:

Go to the source code of this file.

Classes

struct  native< T >
 

Functions

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)
 
template<typename T >
void run_exprtk_benchmark (T &x, T &y, exprtk::expression< T > &expression, const std::string &expr_string)
 
template<typename T , typename NativeFunction >
void run_native_benchmark (T &x, T &y, NativeFunction f, const std::string &expr_string)
 
template<typename T >
bool run_parse_benchmark (exprtk::symbol_table< T > &symbol_table)
 
void pgo_primer ()
 
void perform_file_based_benchmark (const std::string &file_name, const std::size_t &rounds=100000)
 
int main (int argc, char *argv[])
 
std::size_t load_expression_file (const std::string &file_name, std::deque< std::string > &expression_list)
 

Variables

static const std::string expression_list []
 
const std::size_t expression_list_size = sizeof(expression_list) / sizeof(std::string)
 
static const real::type lower_bound_x = real::type(-100.0)
 
static const real::type lower_bound_y = real::type(-100.0)
 
static const real::type upper_bound_x = real::type(+100.0)
 
static const real::type upper_bound_y = real::type(+100.0)
 
static const real::type delta = real::type(0.0111)
 
static const double lower_bound_x_d = -100.0
 
static const double lower_bound_y_d = -100.0
 
static const double upper_bound_x_d = +100.0
 
static const double upper_bound_y_d = +100.0
 
static const double delta_d = 0.0111
 
const real::type pi = 3.14159265358979323846
 
const double pi_d = 3.14159265358979323846
 

Function Documentation

◆ load_expression()

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 
)

Definition at line 72 of file exprtk_real_benchmark.cpp.

74{
76 for (std::size_t i = 0; i < expression_list_size; ++i)
77 {
79 expression.register_symbol_table(symbol_table);
80
81 if (!parser.compile(expression_list[i],expression))
82 {
83 printf("[load_expression] - Parser Error: %s\tExpression: %s\n",
84 parser.error().c_str(),
85 expression_list[i].c_str());
86
87 return false;
88 }
89
90 expr_seq.push_back(expression);
91 }
92
93 return true;
94}
bool register_symbol_table(symbol_table< T > &st)
Definition exprtk.hpp:21726
bool compile(const std::string &expression_string, expression< T > &expr)
Definition exprtk.hpp:24443
std::string error() const
Definition exprtk.hpp:24722
const std::size_t expression_list_size
static const std::string expression_list[]

References exprtk::parser< T >::compile(), exprtk::parser< T >::error(), expression_list, expression_list_size, and exprtk::expression< T >::register_symbol_table().

Referenced by main().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ load_expression_file()

std::size_t load_expression_file ( const std::string &  file_name,
std::deque< std::string > &  expression_list 
)

Definition at line 409 of file exprtk_real_benchmark.cpp.

410{
411 std::ifstream stream(file_name.c_str());
412
413 if (!stream) return 0;
414
415 std::string buffer;
416 buffer.reserve(1024);
417 std::size_t line_count = 0;
418
419 while (std::getline(stream,buffer))
420 {
421 if (buffer.empty())
422 continue;
423 else if ('#' == buffer[0])
424 continue;
425
426 ++line_count;
427 expression_list.push_back(buffer);
428 }
429
430 return line_count;
431}

References expression_list.

Referenced by perform_file_based_benchmark().

Here is the caller graph for this function:

◆ main()

int main ( int  argc,
char *  argv[] 
)

Definition at line 304 of file exprtk_real_benchmark.cpp.

305{
306 if (argc >= 2)
307 {
308 const std::string file_name = argv[1];
309
310 if (argc == 2)
312 else
313 perform_file_based_benchmark(file_name,atoi(argv[2]));
314
315 return 0;
316 }
317
318 pgo_primer();
319
320 real::type x = 0;
321 real::type y = 0;
322
324 symbol_table.add_constants();
325 symbol_table.add_variable("x",x);
326 symbol_table.add_variable("y",y);
327
328 std::deque<exprtk::expression<real::type> > expr_list;
329
330 if (!load_expression(symbol_table,expr_list))
331 {
332 return 1;
333 }
334
335 {
336 std::cout << "--- EXPRTK ---" << std::endl;
337 for (std::size_t i = 0; i < expr_list.size(); ++i)
338 {
339 run_exprtk_benchmark(x,y,expr_list[i],expression_list[i]);
340 }
341 }
342
343 {
344 std::cout << "--- NATIVE ---" << std::endl;
345 double x = 0;
346 double y = 0;
364 }
365
366 {
367 std::cout << "--- PARSE ----" << std::endl;
368 run_parse_benchmark(symbol_table);
369 }
370
371 return 0;
372}
bool add_variable(const std::string &variable_name, T &t, const bool is_constant=false)
Definition exprtk.hpp:20770
void pgo_primer()
void perform_file_based_benchmark(const std::string &file_name, const std::size_t &rounds=100000)
bool run_parse_benchmark(exprtk::symbol_table< T > &symbol_table)
void run_exprtk_benchmark(T &x, T &y, exprtk::expression< T > &expression, const std::string &expr_string)
void run_native_benchmark(T &x, T &y, NativeFunction f, const std::string &expr_string)
bool load_expression(exprtk::symbol_table< T > &symbol_table, Sequence< exprtk::expression< T >, Allocator > &expr_seq)

References exprtk::symbol_table< T >::add_constants(), exprtk::symbol_table< T >::add_variable(), expression_list, load_expression(), perform_file_based_benchmark(), pgo_primer(), run_exprtk_benchmark(), run_native_benchmark(), and run_parse_benchmark().

Here is the call graph for this function:

◆ perform_file_based_benchmark()

void perform_file_based_benchmark ( const std::string &  file_name,
const std::size_t &  rounds = 100000 
)

Definition at line 433 of file exprtk_real_benchmark.cpp.

434{
435 std::deque<std::string> expr_str_list;
436
437 if (0 == load_expression_file(file_name,expr_str_list))
438 {
439 std::cout << "Failed to load any expressions from: " << file_name << "\n";
440 return;
441 }
442
443 typedef exprtk::symbol_table<real::type> symbol_table_t;
444 typedef exprtk::expression<real::type> expression_t;
445 typedef exprtk::parser<real::type> parser_t;
446
447 std::deque<expression_t> expression_list;
448
449 symbol_table_t symbol_table;
450
451 real::type a = 1.1;
452 real::type b = 2.2;
453 real::type c = 3.3;
454 real::type x = 2.123456;
455 real::type y = 3.123456;
456 real::type z = 4.123456;
457 real::type w = 5.123456;
458
459 symbol_table.add_variable("a", a);
460 symbol_table.add_variable("b", b);
461 symbol_table.add_variable("c", c);
462
463 symbol_table.add_variable("x", x);
464 symbol_table.add_variable("y", y);
465 symbol_table.add_variable("z", z);
466 symbol_table.add_variable("w", w);
467
480
481 symbol_table.add_function("poly01", poly01);
482 symbol_table.add_function("poly02", poly02);
483 symbol_table.add_function("poly03", poly03);
484 symbol_table.add_function("poly04", poly04);
485 symbol_table.add_function("poly05", poly05);
486 symbol_table.add_function("poly06", poly06);
487 symbol_table.add_function("poly07", poly07);
488 symbol_table.add_function("poly08", poly08);
489 symbol_table.add_function("poly09", poly09);
490 symbol_table.add_function("poly10", poly10);
491 symbol_table.add_function("poly11", poly11);
492 symbol_table.add_function("poly12", poly12);
493
495 symbol_table.add_variable("e", e, true);
496
497 symbol_table.add_constants();
498
499 {
500 parser_t parser;
501
502 for (std::size_t i = 0; i < expr_str_list.size(); ++i)
503 {
504 expression_t expression;
505 expression.register_symbol_table(symbol_table);
506
507 if (!parser.compile(expr_str_list[i],expression))
508 {
509 printf("[perform_file_based_benchmark] - Parser Error: %s\tExpression: %s\n",
510 parser.error().c_str(),
511 expr_str_list[i].c_str());
512
513 return;
514 }
515
516 expression_list.push_back(expression);
517 }
518 }
519
520 exprtk::timer total_timer;
521
522 real::type single_eval_total_time = 0.0;
523
524 total_timer.start();
525
526 for (std::size_t i = 0; i < expression_list.size(); ++i)
527 {
528 expression_t& e = expression_list[i];
529
530 exprtk::timer timer;
531
532 a = real::type(1.1);
533 b = real::type(2.2);
534 c = real::type(3.3);
535 x = real::type(2.123456);
536 y = real::type(3.123456);
537 z = real::type(4.123456);
538 w = real::type(5.123456);
539
540 timer.start();
541 real::type sum = 0.0;
542
543 for (std::size_t r = 0; r < rounds; ++r)
544 {
545 sum += e.value();
546 std::swap(a,b);
547 std::swap(x,y);
548 }
549
550 timer.stop();
551
552 printf("Expression %3d of %3d %9.3f ns\t%10d ns\t(%30.10f) '%s'\n",
553 static_cast<int>(i + 1),
554 static_cast<int>(expression_list.size()),
555 (timer.time() * 1000000000.0) / (1.0 * rounds),
556 static_cast<int>(timer.time() * 1000000000.0),
557 static_cast<double>(sum),
558 expr_str_list[i].c_str());
559
560 fflush(stdout);
561
562 single_eval_total_time += (timer.time() * 1000000000.0) / (1.0 * rounds);
563 }
564
565 total_timer.stop();
566
567 printf("[*] Number Of Evals: %15.0f\n",
568 rounds * (expression_list.size() * 1.0));
569
570 printf("[*] Total Time: %9.3fsec\n",
571 total_timer.time());
572
573 printf("[*] Total Single Eval Time: %9.3fms\n",
574 static_cast<double>(single_eval_total_time / 1000000.0));
575}
double time() const
Definition exprtk.hpp:43502
static const std::size_t rounds
std::size_t load_expression_file(const std::string &file_name, std::deque< std::string > &expression_list)

References exprtk::details::numeric::constant::e, expression_list, load_expression_file(), rounds, exprtk::timer::start(), exprtk::timer::stop(), and exprtk::timer::time().

Referenced by main().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ pgo_primer()

void pgo_primer ( )

Definition at line 374 of file exprtk_real_benchmark.cpp.

375{
376 static const real::type lower_bound_x = -50.0;
377 static const real::type lower_bound_y = -50.0;
378 static const real::type upper_bound_x = +50.0;
379 static const real::type upper_bound_y = +50.0;
380 static const real::type delta = 0.07;
381
382 real::type total = 0.0;
383
384 for (real::type x = lower_bound_x; x <= upper_bound_x; x += delta)
385 {
386 for (real::type y = lower_bound_y; y <= upper_bound_y; y += delta)
387 {
388 total += native<double>::func00(x,y);
389 total += native<double>::func01(x,y);
390 total += native<double>::func02(x,y);
391 total += native<double>::func03(x,y);
392 total += native<double>::func04(x,y);
393 total += native<double>::func05(x,y);
394 total += native<double>::func06(x,y);
395 total += native<double>::func07(x,y);
396 total += native<double>::func08(x,y);
397 total += native<double>::func09(x,y);
398 total += native<double>::func10(x,y);
399 total += native<double>::func11(x,y);
400 total += native<double>::func12(x,y);
401 total += native<double>::func13(x,y);
402 total += native<double>::func14(x,y);
403 total += native<double>::func15(x,y);
404 total += native<double>::func16(x,y);
405 }
406 }
407}
static const real::type upper_bound_y
static const real::type lower_bound_y
static const real::type delta
static const real::type upper_bound_x
static const real::type lower_bound_x
static T func06(Type x, Type y)
static T func09(Type x, Type y)
static T func16(Type x, Type y)
static T func11(Type x, Type y)
static T func10(Type x, Type y)
static T func08(Type x, Type y)
static T func12(Type x, Type y)
static T func04(Type x, Type y)
static T func02(Type x, Type y)
static T func15(Type x, Type y)
static T func00(Type x, Type y)
static T func03(Type x, Type y)
static T func13(Type x, Type y)
static T func07(Type x, Type y)
static T func05(Type x, Type y)
static T func14(Type x, Type y)
static T func01(Type x, Type y)

References delta, native< T >::func00(), native< T >::func01(), native< T >::func02(), native< T >::func03(), native< T >::func04(), native< T >::func05(), native< T >::func06(), native< T >::func07(), native< T >::func08(), native< T >::func09(), native< T >::func10(), native< T >::func11(), native< T >::func12(), native< T >::func13(), native< T >::func14(), native< T >::func15(), native< T >::func16(), lower_bound_x, lower_bound_y, upper_bound_x, and upper_bound_y.

Referenced by main().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ run_exprtk_benchmark()

template<typename T >
void run_exprtk_benchmark ( T &  x,
T &  y,
exprtk::expression< T > &  expression,
const std::string &  expr_string 
)

Definition at line 97 of file exprtk_real_benchmark.cpp.

100{
101 T total = T(0);
102 unsigned int count = 0;
103
104 exprtk::timer timer;
105 timer.start();
106
107 for (x = lower_bound_x; x <= upper_bound_x; x += delta)
108 {
109 for (y = lower_bound_y; y <= upper_bound_y; y += delta)
110 {
111 total += expression.value();
112 ++count;
113 }
114 }
115
116 timer.stop();
117
118 if (T(0) != total)
119 printf("[exprtk] Total Time:%12.8f Rate:%14.3fevals/sec Expression: %s\n",
120 timer.time(),
121 count / timer.time(),
122 expr_string.c_str());
123 else
124 printf("run_exprtk_benchmark() - Error running benchmark for expression: %s\n",expr_string.c_str());
125}

References delta, lower_bound_x, lower_bound_y, exprtk::timer::start(), exprtk::timer::stop(), exprtk::timer::time(), upper_bound_x, upper_bound_y, and exprtk::expression< T >::value().

Referenced by main().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ run_native_benchmark()

template<typename T , typename NativeFunction >
void run_native_benchmark ( T &  x,
T &  y,
NativeFunction  f,
const std::string &  expr_string 
)

Definition at line 130 of file exprtk_real_benchmark.cpp.

131{
132 T total = T(0);
133 unsigned int count = 0;
134
135 exprtk::timer timer;
136 timer.start();
137
138 for (x = lower_bound_x_d; x <= upper_bound_x_d; x += delta_d)
139 {
140 for (y = lower_bound_y_d; y <= upper_bound_y_d; y += delta_d)
141 {
142 total += f(x,y);
143 ++count;
144 }
145 }
146
147 timer.stop();
148
149 if (T(0) != total)
150 printf("[native] Total Time:%12.8f Rate:%14.3fevals/sec Expression: %s\n",
151 timer.time(),
152 count / timer.time(),
153 expr_string.c_str());
154 else
155 printf("run_native_benchmark() - Error running benchmark for expression: %s\n",expr_string.c_str());
156}
static const double upper_bound_y_d
static const double lower_bound_y_d
static const double delta_d
static const double lower_bound_x_d
static const double upper_bound_x_d

References delta_d, lower_bound_x_d, lower_bound_y_d, exprtk::timer::start(), exprtk::timer::stop(), exprtk::timer::time(), upper_bound_x_d, and upper_bound_y_d.

Referenced by main().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ run_parse_benchmark()

template<typename T >
bool run_parse_benchmark ( exprtk::symbol_table< T > &  symbol_table)

Definition at line 159 of file exprtk_real_benchmark.cpp.

160{
161 static const std::size_t rounds = 100000;
164
165 expression.register_symbol_table(symbol_table);
166
167 for (std::size_t i = 0; i < expression_list_size; ++i)
168 {
169 exprtk::timer timer;
170 timer.start();
171
172 for (std::size_t r = 0; r < rounds; ++r)
173 {
174 if (!parser.compile(expression_list[i],expression))
175 {
176 printf("[run_parse_benchmark] - Parser Error: %s\tExpression: %s\n",
177 parser.error().c_str(),
178 expression_list[i].c_str());
179
180 return false;
181 }
182 }
183
184 timer.stop();
185
186 printf("[parse] Total Time:%12.8f Rate:%14.3fparse/sec Expression: %s\n",
187 timer.time(),
188 rounds / timer.time(),
189 expression_list[i].c_str());
190 }
191
192 return true;
193}

References exprtk::parser< T >::compile(), exprtk::parser< T >::error(), expression_list, expression_list_size, exprtk::expression< T >::register_symbol_table(), rounds, exprtk::timer::start(), exprtk::timer::stop(), and exprtk::timer::time().

Referenced by main().

Here is the call graph for this function:
Here is the caller graph for this function:

Variable Documentation

◆ delta

const real::type delta = real::type(0.0111)
static

Definition at line 60 of file exprtk_real_benchmark.cpp.

Referenced by pgo_primer(), and run_exprtk_benchmark().

◆ delta_d

const double delta_d = 0.0111
static

Definition at line 66 of file exprtk_real_benchmark.cpp.

Referenced by run_native_benchmark().

◆ expression_list

const std::string expression_list[]
static
Initial value:
=
{
"(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"
}

Definition at line 32 of file exprtk_real_benchmark.cpp.

33{
34 "(y + x)",
35 "2 * (y + x)",
36 "(2 * y + 2 * x)",
37 "((1.23 * x^2) / y) - 123.123",
38 "(y + x / y) * (x - y / x)",
39 "x / ((x + y) + (x - y)) / y",
40 "1 - ((x * y) + (y / x)) - 3",
41 "(5.5 + x) + (2 * x - 2 / 3 * y) * (x / 3 + y / 4) + (y + 7.7)",
42 "1.1x^1 + 2.2y^2 - 3.3x^3 + 4.4y^15 - 5.5x^23 + 6.6y^55",
43 "sin(2 * x) + cos(pi / y)",
44 "1 - sin(2 * x) + cos(pi / y)",
45 "sqrt(111.111 - sin(2 * x) + cos(pi / y) / 333.333)",
46 "(x^2 / sin(2 * pi / y)) - x / 2",
47 "x + (cos(y - sin(2 / x * pi)) - sin(x - cos(2 * y / pi))) - y",
48 "clamp(-1.0, sin(2 * pi * x) + cos(y / 2 * pi), +1.0)",
49 "max(3.33, min(sqrt(1 - sin(2 * x) + cos(pi / y) / 3), 1.11))",
50 "if((y + (x * 2.2)) <= (x + y + 1.1), x - y, x * y) + 2 * pi / x"
51};

Referenced by load_expression(), load_expression_file(), main(), main(), perform_file_based_benchmark(), pgo_primer(), run_parse_benchmark(), run_test05(), run_test09(), run_test10(), run_test14(), run_test15(), run_test16(), run_test17(), run_test18(), run_test19(), run_test21(), exprtk::parser< T >::simplify(), and test_gen().

◆ expression_list_size

const std::size_t expression_list_size = sizeof(expression_list) / sizeof(std::string)

◆ lower_bound_x

const real::type lower_bound_x = real::type(-100.0)
static

Definition at line 56 of file exprtk_real_benchmark.cpp.

Referenced by pgo_primer(), and run_exprtk_benchmark().

◆ lower_bound_x_d

const double lower_bound_x_d = -100.0
static

Definition at line 62 of file exprtk_real_benchmark.cpp.

Referenced by run_native_benchmark().

◆ lower_bound_y

const real::type lower_bound_y = real::type(-100.0)
static

Definition at line 57 of file exprtk_real_benchmark.cpp.

Referenced by pgo_primer(), and run_exprtk_benchmark().

◆ lower_bound_y_d

const double lower_bound_y_d = -100.0
static

Definition at line 63 of file exprtk_real_benchmark.cpp.

Referenced by run_native_benchmark().

◆ pi

const real::type pi = 3.14159265358979323846

◆ pi_d

const double pi_d = 3.14159265358979323846

◆ upper_bound_x

const real::type upper_bound_x = real::type(+100.0)
static

Definition at line 58 of file exprtk_real_benchmark.cpp.

Referenced by pgo_primer(), and run_exprtk_benchmark().

◆ upper_bound_x_d

const double upper_bound_x_d = +100.0
static

Definition at line 64 of file exprtk_real_benchmark.cpp.

Referenced by run_native_benchmark().

◆ upper_bound_y

const real::type upper_bound_y = real::type(+100.0)
static

Definition at line 59 of file exprtk_real_benchmark.cpp.

Referenced by pgo_primer(), and run_exprtk_benchmark().

◆ upper_bound_y_d

const double upper_bound_y_d = +100.0
static

Definition at line 65 of file exprtk_real_benchmark.cpp.

Referenced by run_native_benchmark().