34 "((1.23 * x^2) / y) - 123.123",
35 "(y + x / y) * (x - y / x)",
36 "x / ((x + y) + (x - y)) / y",
37 "1 - ((x * y) + (y / x)) - 3",
38 "(5.5 + x) + (2 * x - 2 / 3 * y) * (x / 3 + y / 4) + (y + 7.7)",
39 "1.1x^1 + 2.2y^2 - 3.3x^3 + 4.4y^15 - 5.5x^23 + 6.6y^55",
40 "sin(2 * x) + cos(pi / y)",
41 "1 - sin(2 * x) + cos(pi / y)",
42 "sqrt(111.111 - sin(2 * x) + cos(pi / y) / 333.333)",
43 "(x^2 / sin(2 * pi / y)) - x / 2",
44 "x + (cos(y - sin(2 / x * pi)) - sin(x - cos(2 * y / pi))) - y",
45 "clamp(-1.0, sin(2 * pi * x) + cos(y / 2 * pi), +1.0)",
46 "max(3.33, min(sqrt(1 - sin(2 * x) + cos(pi / y) / 3), 1.11))",
47 "if((y + (x * 2.2)) <= (x + y + 1.1), x - y, x * y) + 2 * pi / x" 61 template <
typename,
typename>
class Sequence>
74 printf(
"[load_expression] - Parser Error: %s\tExpression: %s\n",
81 expr_seq.push_back(expression);
93 unsigned int count = 0;
102 total += expression.
value();
110 printf(
"[exprtk] Total Time:%12.8f Rate:%14.3fevals/sec Expression: %s\n",
112 count / timer.
time(),
113 expr_string.c_str());
115 printf(
"run_exprtk_benchmark() - Error running benchmark for expression: %s\n",expr_string.c_str());
120 template <
typename T,
typename NativeFunction>
124 unsigned int count = 0;
141 printf(
"[native] Total Time:%12.8f Rate:%14.3fevals/sec Expression: %s\n",
143 count / timer.
time(),
144 expr_string.c_str());
146 printf(
"run_native_benchmark() - Error running benchmark for expression: %s\n",expr_string.c_str());
149 template <
typename T>
152 static const std::size_t rounds = 100000;
163 for (std::size_t r = 0; r < rounds; ++r)
167 printf(
"[run_parse_benchmark] - Parser Error: %s\tExpression: %s\n",
177 printf(
"[parse] Total Time:%12.8f Rate:%14.3fparse/sec Expression: %s\n",
179 rounds / timer.
time(),
186 const double pi = 3.141592653589793238462643383279502;
188 template <
typename T>
196 return (
x + y) / T(2);
201 return ((v < l) ? l : ((v > u) ? u : v));
211 return T(2) * (y +
x);
216 return (T(2) * y + T(2) *
x);
221 return ((T(1.23) * (
x *
x)) / y) - T(123.123);
226 return (y +
x / y) * (
x - y /
x);
231 return x / ((
x + y) + (
x - y)) / y;
236 return T(1) - ((
x * y) + (y /
x)) - T(3);
241 return (T(5.5) +
x) + (T(2) *
x - T(2) / T(3) * y) * (
x / T(3) + y / T(4)) + (y + T(7.7));
247 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)));
252 return std::sin(T(2) *
x) + std::cos(
pi / y);
257 return T(1) - std::sin(T(2) *
x) + std::cos(
pi / y);
262 return std::sqrt(T(111.111) - std::sin(T(2) *
x) + std::cos(
pi / y) / T(333.333));
267 return ((
x *
x) / std::sin(T(2) *
pi / y)) -
x / T(2);
272 return (
x + (std::cos(y - std::sin(T(2) /
x *
pi)) - std::sin(
x - std::cos(T(2) * y /
pi))) - y);
277 return clamp(T(-1), std::sin(T(2) *
pi *
x) + std::cos(y / T(2) *
pi), + T(1));
282 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)));
287 return (((y + (
x * T(2.2))) <= (
x + y + T(1.1))) ?
x - y :
x * y) + T(2) *
pi /
x;
294 int main(
int argc,
char* argv[])
318 std::deque<exprtk::expression<double> > compiled_expr_list;
326 std::cout <<
"--- EXPRTK ---" << std::endl;
327 for (std::size_t i = 0; i < compiled_expr_list.size(); ++i)
334 std::cout <<
"--- NATIVE ---" << std::endl;
355 std::cout <<
"--- PARSE ----" << std::endl;
364 exprtk::pgo_primer<double>();
366 static const double lower_bound_x = -50.0;
367 static const double lower_bound_y = -50.0;
368 static const double upper_bound_x = +50.0;
369 static const double upper_bound_y = +50.0;
370 static const double delta = 0.07;
374 for (
double x = lower_bound_x;
x <= upper_bound_x;
x += delta)
376 for (
double y = lower_bound_y; y <= upper_bound_y; y += delta)
401 std::ifstream stream(file_name.c_str());
403 if (!stream)
return 0;
407 std::size_t line_count = 0;
409 while (std::getline(stream,
buffer))
413 else if (
'#' ==
buffer[0])
417 expression_list.push_back(
buffer);
425 std::deque<std::string> expr_str_list;
429 std::cout <<
"Failed to load any expressions from: " << file_name <<
"\n";
437 std::deque<expression_t> expression_list;
449 symbol_table.add_variable(
"a", a);
450 symbol_table.add_variable(
"b", b);
451 symbol_table.add_variable(
"c", c);
453 symbol_table.add_variable(
"x",
x);
454 symbol_table.add_variable(
"y", y);
455 symbol_table.add_variable(
"z",
z);
456 symbol_table.add_variable(
"w", w);
471 symbol_table.add_function(
"poly01", poly01);
472 symbol_table.add_function(
"poly02", poly02);
473 symbol_table.add_function(
"poly03", poly03);
474 symbol_table.add_function(
"poly04", poly04);
475 symbol_table.add_function(
"poly05", poly05);
476 symbol_table.add_function(
"poly06", poly06);
477 symbol_table.add_function(
"poly07", poly07);
478 symbol_table.add_function(
"poly08", poly08);
479 symbol_table.add_function(
"poly09", poly09);
480 symbol_table.add_function(
"poly10", poly10);
481 symbol_table.add_function(
"poly11", poly11);
482 symbol_table.add_function(
"poly12", poly12);
485 symbol_table.add_variable(
"e",
e,
true);
487 symbol_table.add_constants();
492 for (std::size_t i = 0; i < expr_str_list.size(); ++i)
497 if (!
parser.compile(expr_str_list[i],expression))
499 printf(
"[perform_file_based_benchmark] - Parser Error: %s\tExpression: %s\n",
501 expr_str_list[i].c_str());
506 expression_list.push_back(expression);
513 double single_eval_total_time = 0.0;
517 for (std::size_t i = 0; i < expression_list.size(); ++i)
532 for (std::size_t r = 0; r < rounds; ++r)
534 sum += expression.value();
541 printf(
"Expression %3d of %3d %9.3f ns\t%10d ns\t(%30.10f) '%s'\n",
542 static_cast<int>(i + 1),
543 static_cast<int>(expression_list.size()),
544 (timer.
time() * 1000000000.0) / (1.0 * rounds),
545 static_cast<int>(timer.
time() * 1000000000.0),
547 expr_str_list[i].c_str());
551 single_eval_total_time += (timer.
time() * 1000000000.0) / (1.0 * rounds);
556 printf(
"[*] Number Of Evals: %15.0f\n",
557 rounds * (expression_list.size() * 1.0));
559 printf(
"[*] Total Time: %9.3fsec\n",
562 printf(
"[*] Total Single Eval Time: %9.3fms\n",
563 single_eval_total_time / 1000000.0);