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

Go to the source code of this file.

Classes

struct  native< T >
 

Typedefs

typedef mpfr::mpreal numeric_type
 

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

const std::string global_expression_list []
 
const std::size_t global_expression_list_size = sizeof(global_expression_list) / sizeof(std::string)
 
static const numeric_type global_lower_bound_x = -100.0
 
static const numeric_type global_lower_bound_y = -100.0
 
static const numeric_type global_upper_bound_x = +100.0
 
static const numeric_type global_upper_bound_y = +100.0
 
static const numeric_type global_delta = 0.1111
 
const numeric_type pi = 3.14159265358979323846
 

Typedef Documentation

◆ numeric_type

typedef mpfr::mpreal numeric_type

Definition at line 32 of file exprtk_mpfr_benchmark.cpp.

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 67 of file exprtk_mpfr_benchmark.cpp.

69{
71
72 for (std::size_t i = 0; i < global_expression_list_size; ++i)
73 {
75 expression.register_symbol_table(symbol_table);
76
77 if (!parser.compile(global_expression_list[i],expression))
78 {
79 printf("[load_expression] - Parser Error: %s\tExpression: %s\n",
80 parser.error().c_str(),
81 global_expression_list[i].c_str());
82
83 return false;
84 }
85
86 expr_seq.push_back(expression);
87 }
88
89 return true;
90}
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::string global_expression_list[]
const std::size_t global_expression_list_size

References exprtk::parser< T >::compile(), exprtk::parser< T >::error(), global_expression_list, global_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 
)
inline

Definition at line 402 of file exprtk_mpfr_benchmark.cpp.

403{
404 std::ifstream stream(file_name.c_str());
405
406 if (!stream) return 0;
407
408 std::string buffer;
409 buffer.reserve(1024);
410 std::size_t line_count = 0;
411
412 while (std::getline(stream,buffer))
413 {
414 if (buffer.empty())
415 continue;
416 else if ('#' == buffer[0])
417 continue;
418
419 ++line_count;
420 expression_list.push_back(buffer);
421 }
422
423 return line_count;
424}
static const std::string expression_list[]

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 299 of file exprtk_mpfr_benchmark.cpp.

300{
301 mpfr::mpreal::set_default_prec(512);
302
303 if (argc >= 2)
304 {
305 const std::string file_name = argv[1];
306
307 if (argc == 2)
309 else
310 perform_file_based_benchmark(file_name,atoi(argv[2]));
311
312 return 0;
313 }
314
315 numeric_type x = 0;
316 numeric_type y = 0;
317
319 symbol_table.add_constants();
320 symbol_table.add_variable("x",x);
321 symbol_table.add_variable("y",y);
322
323 std::deque<exprtk::expression<numeric_type> > compiled_expr_list;
324
325 if (!load_expression(symbol_table,compiled_expr_list))
326 {
327 return 1;
328 }
329
330 {
331 std::cout << "--- EXPRTK ---" << std::endl;
332 for (std::size_t i = 0; i < compiled_expr_list.size(); ++i)
333 {
334 run_exprtk_benchmark(x,y,compiled_expr_list[i],global_expression_list[i]);
335 }
336 }
337
338 {
339 std::cout << "--- NATIVE ---" << std::endl;
357 }
358
359 {
360 std::cout << "--- PARSE ----" << std::endl;
361 run_parse_benchmark(symbol_table);
362 }
363
364 return 0;
365}
bool add_variable(const std::string &variable_name, T &t, const bool is_constant=false)
Definition exprtk.hpp:20770
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)
mpfr::mpreal numeric_type
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(), global_expression_list, load_expression(), perform_file_based_benchmark(), 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 426 of file exprtk_mpfr_benchmark.cpp.

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

368{
369 static const double lower_bound_x = -50.0;
370 static const double lower_bound_y = -50.0;
371 static const double upper_bound_x = +50.0;
372 static const double upper_bound_y = +50.0;
373 static const double delta = 0.07;
374
375 numeric_type total = 0.0;
376
377 for (numeric_type x = lower_bound_x; x <= upper_bound_x; x += delta)
378 {
379 for (numeric_type y = lower_bound_y; y <= upper_bound_y; y += delta)
380 {
381 total += native<numeric_type>::func00(x,y);
382 total += native<numeric_type>::func01(x,y);
383 total += native<numeric_type>::func02(x,y);
384 total += native<numeric_type>::func03(x,y);
385 total += native<numeric_type>::func04(x,y);
386 total += native<numeric_type>::func05(x,y);
387 total += native<numeric_type>::func06(x,y);
388 total += native<numeric_type>::func07(x,y);
389 total += native<numeric_type>::func08(x,y);
390 total += native<numeric_type>::func09(x,y);
391 total += native<numeric_type>::func10(x,y);
392 total += native<numeric_type>::func11(x,y);
393 total += native<numeric_type>::func12(x,y);
394 total += native<numeric_type>::func13(x,y);
395 total += native<numeric_type>::func14(x,y);
396 total += native<numeric_type>::func15(x,y);
397 total += native<numeric_type>::func16(x,y);
398 }
399 }
400}
static const double upper_bound_y
static const double upper_bound_x
static const double delta
static const double lower_bound_x
static const double lower_bound_y
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.

Here is the call 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 93 of file exprtk_mpfr_benchmark.cpp.

96{
97 T total = T(0);
98 unsigned int count = 0;
99
100 exprtk::timer timer;
101 timer.start();
102
104 {
106 {
107 total += expression.value();
108 ++count;
109 }
110 }
111
112 timer.stop();
113
114 if (T(0) != total)
115 printf("[exprtk] Total Time:%12.8f Rate:%14.3fevals/sec Expression: %s\n",
116 timer.time(),
117 count / timer.time(),
118 expr_string.c_str());
119 else
120 printf("run_exprtk_benchmark() - Error running benchmark for expression: %s\n",expr_string.c_str());
121}
static const numeric_type global_delta
static const numeric_type global_lower_bound_x
static const numeric_type global_upper_bound_x
static const numeric_type global_upper_bound_y
static const numeric_type global_lower_bound_y

References global_delta, global_lower_bound_x, global_lower_bound_y, global_upper_bound_x, global_upper_bound_y, exprtk::timer::start(), exprtk::timer::stop(), exprtk::timer::time(), 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 126 of file exprtk_mpfr_benchmark.cpp.

127{
128 T total = T(0);
129 unsigned int count = 0;
130
131 exprtk::timer timer;
132 timer.start();
133
135 {
137 {
138 total += f(x,y);
139 ++count;
140 }
141 }
142
143 timer.stop();
144
145 if (T(0) != total)
146 printf("[native] Total Time:%12.8f Rate:%14.3fevals/sec Expression: %s\n",
147 timer.time(),
148 count / timer.time(),
149 expr_string.c_str());
150 else
151 printf("run_native_benchmark() - Error running benchmark for expression: %s\n",expr_string.c_str());
152}

References global_delta, global_lower_bound_x, global_lower_bound_y, global_upper_bound_x, global_upper_bound_y, 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:

◆ run_parse_benchmark()

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

Definition at line 155 of file exprtk_mpfr_benchmark.cpp.

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

References exprtk::parser< T >::compile(), exprtk::parser< T >::error(), global_expression_list, global_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

◆ global_delta

const numeric_type global_delta = 0.1111
static

Definition at line 61 of file exprtk_mpfr_benchmark.cpp.

Referenced by run_exprtk_benchmark(), and run_native_benchmark().

◆ global_expression_list

const std::string global_expression_list[]
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 34 of file exprtk_mpfr_benchmark.cpp.

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

Referenced by load_expression(), main(), and run_parse_benchmark().

◆ global_expression_list_size

const std::size_t global_expression_list_size = sizeof(global_expression_list) / sizeof(std::string)

Definition at line 55 of file exprtk_mpfr_benchmark.cpp.

Referenced by load_expression(), and run_parse_benchmark().

◆ global_lower_bound_x

const numeric_type global_lower_bound_x = -100.0
static

Definition at line 57 of file exprtk_mpfr_benchmark.cpp.

Referenced by run_exprtk_benchmark(), and run_native_benchmark().

◆ global_lower_bound_y

const numeric_type global_lower_bound_y = -100.0
static

Definition at line 58 of file exprtk_mpfr_benchmark.cpp.

Referenced by run_exprtk_benchmark(), and run_native_benchmark().

◆ global_upper_bound_x

const numeric_type global_upper_bound_x = +100.0
static

Definition at line 59 of file exprtk_mpfr_benchmark.cpp.

Referenced by run_exprtk_benchmark(), and run_native_benchmark().

◆ global_upper_bound_y

const numeric_type global_upper_bound_y = +100.0
static

Definition at line 60 of file exprtk_mpfr_benchmark.cpp.

Referenced by run_exprtk_benchmark(), and run_native_benchmark().

◆ pi

const numeric_type pi = 3.14159265358979323846

Definition at line 191 of file exprtk_mpfr_benchmark.cpp.