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"
66 template <
typename,
typename>
class Sequence>
79 printf(
"[load_expression] - Parser Error: %s\tExpression: %s\n",
80 parser.
error().c_str(),
86 expr_seq.push_back(expression);
95 const std::string& expr_string)
98 unsigned int count = 0;
107 total += expression.
value();
115 printf(
"[exprtk] Total Time:%12.8f Rate:%14.3fevals/sec Expression: %s\n",
117 count / timer.
time(),
118 expr_string.c_str());
120 printf(
"run_exprtk_benchmark() - Error running benchmark for expression: %s\n",expr_string.c_str());
123template <
typename T>
struct native;
125template <
typename T,
typename NativeFunction>
129 unsigned int count = 0;
146 printf(
"[native] Total Time:%12.8f Rate:%14.3fevals/sec Expression: %s\n",
148 count / timer.
time(),
149 expr_string.c_str());
151 printf(
"run_native_benchmark() - Error running benchmark for expression: %s\n",expr_string.c_str());
157 static const std::size_t
rounds = 100000;
168 for (std::size_t r = 0; r <
rounds; ++r)
172 printf(
"[run_parse_benchmark] - Parser Error: %s\tExpression: %s\n",
173 parser.
error().c_str(),
182 printf(
"[parse] Total Time:%12.8f Rate:%14.3fparse/sec Expression: %s\n",
201 return (x + y) / T(2);
206 return ((v < l) ? l : ((v > u) ? u : v));
216 return T(2) * (y + x);
221 return (T(2) * y + T(2) * x);
226 return ((T(1.23) * (x * x)) / y) - T(123.123);
231 return (y + x / y) * (x - y / x);
236 return x / ((x + y) + (x - y)) / y;
241 return T(1) - ((x * y) + (y / x)) - T(3);
246 return (T(5.5) + x) + (T(2) * x - T(2) / T(3) * y) * (x / T(3) + y / T(4)) + (y + T(7.7));
252 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)));
257 return mpfr::sin(T(2) * x) + mpfr::cos(
pi / y);
262 return T(1) - mpfr::sin(T(2) * x) + mpfr::cos(
pi / y);
267 return mpfr::sqrt(T(111.111) - mpfr::sin(T(2) * x) + mpfr::cos(
pi / y) / T(333.333));
272 return ((x * x) / mpfr::sin(T(2) *
pi / y)) - x / T(2);
277 return (x + (mpfr::cos(y - mpfr::sin(T(2) / x *
pi)) - mpfr::sin(x - mpfr::cos(T(2) * y /
pi))) - y);
282 return clamp(T(-1), mpfr::sin(T(2) *
pi * x) + mpfr::cos(y / T(2) *
pi), + T(1));
287 return mpfr::max(T(3.33), mpfr::min(sqrt(T(1) - mpfr::sin(T(2) * x) + mpfr::cos(
pi / y) / T(3)), T(1.11)));
292 return (((y + (x * T(2.2))) <= (x + y + T(1.1))) ? x - y : x * y) + T(2) *
pi / x;
299int main(
int argc,
char* argv[])
301 mpfr::mpreal::set_default_prec(512);
305 const std::string file_name = argv[1];
323 std::deque<exprtk::expression<numeric_type> > compiled_expr_list;
331 std::cout <<
"--- EXPRTK ---" << std::endl;
332 for (std::size_t i = 0; i < compiled_expr_list.size(); ++i)
339 std::cout <<
"--- NATIVE ---" << std::endl;
360 std::cout <<
"--- PARSE ----" << std::endl;
373 static const double delta = 0.07;
404 std::ifstream stream(file_name.c_str());
406 if (!stream)
return 0;
409 buffer.reserve(1024);
410 std::size_t line_count = 0;
412 while (std::getline(stream,buffer))
416 else if (
'#' == buffer[0])
428 std::deque<std::string> expr_str_list;
432 std::cout <<
"Failed to load any expressions from: " << file_name <<
"\n";
442 symbol_table_t symbol_table;
452 symbol_table.add_variable(
"a", a);
453 symbol_table.add_variable(
"b", b);
454 symbol_table.add_variable(
"c", c);
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);
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);
488 symbol_table.add_variable(
"e", e,
true);
490 symbol_table.add_constants();
495 for (std::size_t i = 0; i < expr_str_list.size(); ++i)
497 expression_t expression;
498 expression.register_symbol_table(symbol_table);
500 if (!parser.compile(expr_str_list[i],expression))
502 printf(
"[perform_file_based_benchmark] - Parser Error: %s\tExpression: %s\n",
503 parser.error().c_str(),
504 expr_str_list[i].c_str());
516 double single_eval_total_time = 0.0;
534 for (std::size_t r = 0; r <
rounds; ++r)
536 sum += expression.value();
543 printf(
"Expression %3d of %3d %9.3f ns\t%10d ns\t(%.30s) '%s'\n",
544 static_cast<int>(i + 1),
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());
553 single_eval_total_time += (timer.
time() * 1000000000.0) / (1.0 *
rounds);
558 printf(
"[*] Number Of Evals: %15.0f\n",
561 printf(
"[*] Total Time: %9.3fsec\n",
564 printf(
"[*] Total Single Eval Time: %9.3fms\n",
565 single_eval_total_time / 1000000.0);
bool register_symbol_table(symbol_table< T > &st)
bool compile(const std::string &expression_string, expression< T > &expr)
std::string error() const
bool add_variable(const std::string &variable_name, T &t, const bool is_constant=false)
static const std::size_t rounds
const std::string global_expression_list[]
static const numeric_type global_delta
void perform_file_based_benchmark(const std::string &file_name, const std::size_t &rounds=100000)
static const numeric_type global_lower_bound_x
bool run_parse_benchmark(exprtk::symbol_table< T > &symbol_table)
std::size_t load_expression_file(const std::string &file_name, std::deque< std::string > &expression_list)
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)
static const numeric_type global_upper_bound_x
static const numeric_type global_upper_bound_y
bool load_expression(exprtk::symbol_table< T > &symbol_table, Sequence< exprtk::expression< T >, Allocator > &expr_seq)
const std::size_t global_expression_list_size
static const numeric_type global_lower_bound_y
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 const std::string expression_list[]
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 clamp(const Type l, const Type v, const Type u)
static T func02(Type x, Type y)
exprtk::details::functor_t< T > functor_t
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 avg(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)