exprtk_benchmark.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564
  1. /*
  2. **************************************************************
  3. * C++ Mathematical Expression Toolkit Library *
  4. * *
  5. * ExprTk vs Native Benchmarks *
  6. * Author: Arash Partow (1999-2020) *
  7. * URL: http://www.partow.net/programming/exprtk/index.html *
  8. * *
  9. * Copyright notice: *
  10. * Free use of the Mathematical Expression Toolkit Library is *
  11. * permitted under the guidelines and in accordance with the *
  12. * most current version of the MIT License. *
  13. * http://www.opensource.org/licenses/MIT *
  14. * *
  15. **************************************************************
  16. */
  17. #include <cstdio>
  18. #include <cmath>
  19. #include <iostream>
  20. #include <fstream>
  21. #include <string>
  22. #include <deque>
  23. #include "exprtk.hpp"
  24. const std::string global_expression_list[]
  25. = {
  26. "(y + x)",
  27. "2 * (y + x)",
  28. "(2 * y + 2 * x)",
  29. "((1.23 * x^2) / y) - 123.123",
  30. "(y + x / y) * (x - y / x)",
  31. "x / ((x + y) + (x - y)) / y",
  32. "1 - ((x * y) + (y / x)) - 3",
  33. "(5.5 + x) + (2 * x - 2 / 3 * y) * (x / 3 + y / 4) + (y + 7.7)",
  34. "1.1x^1 + 2.2y^2 - 3.3x^3 + 4.4y^15 - 5.5x^23 + 6.6y^55",
  35. "sin(2 * x) + cos(pi / y)",
  36. "1 - sin(2 * x) + cos(pi / y)",
  37. "sqrt(111.111 - sin(2 * x) + cos(pi / y) / 333.333)",
  38. "(x^2 / sin(2 * pi / y)) - x / 2",
  39. "x + (cos(y - sin(2 / x * pi)) - sin(x - cos(2 * y / pi))) - y",
  40. "clamp(-1.0, sin(2 * pi * x) + cos(y / 2 * pi), +1.0)",
  41. "max(3.33, min(sqrt(1 - sin(2 * x) + cos(pi / y) / 3), 1.11))",
  42. "if((y + (x * 2.2)) <= (x + y + 1.1), x - y, x * y) + 2 * pi / x"
  43. };
  44. const std::size_t global_expression_list_size = sizeof(global_expression_list) / sizeof(std::string);
  45. static const double global_lower_bound_x = -100.0;
  46. static const double global_lower_bound_y = -100.0;
  47. static const double global_upper_bound_x = +100.0;
  48. static const double global_upper_bound_y = +100.0;
  49. static const double global_delta = 0.0111;
  50. template <typename T,
  51. typename Allocator,
  52. template <typename,typename> class Sequence>
  53. bool load_expression(exprtk::symbol_table<T>& symbol_table,
  54. Sequence<exprtk::expression<T>,Allocator>& expr_seq)
  55. {
  56. exprtk::parser<double> parser;
  57. for (std::size_t i = 0; i < global_expression_list_size; ++i)
  58. {
  59. exprtk::expression<double> expression;
  60. expression.register_symbol_table(symbol_table);
  61. if (!parser.compile(global_expression_list[i],expression))
  62. {
  63. printf("[load_expression] - Parser Error: %s\tExpression: %s\n",
  64. parser.error().c_str(),
  65. global_expression_list[i].c_str());
  66. return false;
  67. }
  68. expr_seq.push_back(expression);
  69. }
  70. return true;
  71. }
  72. template <typename T>
  73. void run_exprtk_benchmark(T& x, T& y,
  74. exprtk::expression<T>& expression,
  75. const std::string& expr_string)
  76. {
  77. T total = T(0);
  78. unsigned int count = 0;
  79. exprtk::timer timer;
  80. timer.start();
  81. for (x = global_lower_bound_x; x <= global_upper_bound_x; x += global_delta)
  82. {
  83. for (y = global_lower_bound_y; y <= global_upper_bound_y; y += global_delta)
  84. {
  85. total += expression.value();
  86. ++count;
  87. }
  88. }
  89. timer.stop();
  90. if (T(0) != total)
  91. printf("[exprtk] Total Time:%12.8f Rate:%14.3fevals/sec Expression: %s\n",
  92. timer.time(),
  93. count / timer.time(),
  94. expr_string.c_str());
  95. else
  96. printf("run_exprtk_benchmark() - Error running benchmark for expression: %s\n",expr_string.c_str());
  97. }
  98. template <typename T> struct native;
  99. template <typename T, typename NativeFunction>
  100. void run_native_benchmark(T& x, T& y, NativeFunction f, const std::string& expr_string)
  101. {
  102. T total = T(0);
  103. unsigned int count = 0;
  104. exprtk::timer timer;
  105. timer.start();
  106. for (x = global_lower_bound_x; x <= global_upper_bound_x; x += global_delta)
  107. {
  108. for (y = global_lower_bound_y; y <= global_upper_bound_y; y += global_delta)
  109. {
  110. total += f(x,y);
  111. ++count;
  112. }
  113. }
  114. timer.stop();
  115. if (T(0) != total)
  116. printf("[native] Total Time:%12.8f Rate:%14.3fevals/sec Expression: %s\n",
  117. timer.time(),
  118. count / timer.time(),
  119. expr_string.c_str());
  120. else
  121. printf("run_native_benchmark() - Error running benchmark for expression: %s\n",expr_string.c_str());
  122. }
  123. template <typename T>
  124. bool run_parse_benchmark(exprtk::symbol_table<T>& symbol_table)
  125. {
  126. static const std::size_t rounds = 100000;
  127. exprtk::parser<double> parser;
  128. exprtk::expression<double> expression;
  129. expression.register_symbol_table(symbol_table);
  130. for (std::size_t i = 0; i < global_expression_list_size; ++i)
  131. {
  132. exprtk::timer timer;
  133. timer.start();
  134. for (std::size_t r = 0; r < rounds; ++r)
  135. {
  136. if (!parser.compile(global_expression_list[i],expression))
  137. {
  138. printf("[run_parse_benchmark] - Parser Error: %s\tExpression: %s\n",
  139. parser.error().c_str(),
  140. global_expression_list[i].c_str());
  141. return false;
  142. }
  143. }
  144. timer.stop();
  145. printf("[parse] Total Time:%12.8f Rate:%14.3fparse/sec Expression: %s\n",
  146. timer.time(),
  147. rounds / timer.time(),
  148. global_expression_list[i].c_str());
  149. }
  150. return true;
  151. }
  152. const double pi = 3.141592653589793238462643383279502;
  153. template <typename T>
  154. struct native
  155. {
  156. typedef typename exprtk::details::functor_t<T> functor_t;
  157. typedef typename functor_t::Type Type;
  158. static inline T avg(Type x, Type y)
  159. {
  160. return (x + y) / T(2);
  161. }
  162. static inline T clamp(const Type l, const Type v, const Type u)
  163. {
  164. return ((v < l) ? l : ((v > u) ? u : v));
  165. }
  166. static inline T func00(Type x, Type y)
  167. {
  168. return (y + x);
  169. }
  170. static inline T func01(Type x, Type y)
  171. {
  172. return T(2) * (y + x);
  173. }
  174. static inline T func02(Type x, Type y)
  175. {
  176. return (T(2) * y + T(2) * x);
  177. }
  178. static inline T func03(Type x, Type y)
  179. {
  180. return ((T(1.23) * (x * x)) / y) - T(123.123);
  181. }
  182. static inline T func04(Type x, Type y)
  183. {
  184. return (y + x / y) * (x - y / x);
  185. }
  186. static inline T func05(Type x, Type y)
  187. {
  188. return x / ((x + y) + (x - y)) / y;
  189. }
  190. static inline T func06(Type x, Type y)
  191. {
  192. return T(1) - ((x * y) + (y / x)) - T(3);
  193. }
  194. static inline T func07(Type x, Type y)
  195. {
  196. return (T(5.5) + x) + (T(2) * x - T(2) / T(3) * y) * (x / T(3) + y / T(4)) + (y + T(7.7));
  197. }
  198. static inline T func08(Type x, Type y)
  199. {
  200. using namespace std;
  201. 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)));
  202. }
  203. static inline T func09(Type x, Type y)
  204. {
  205. return std::sin(T(2) * x) + std::cos(pi / y);
  206. }
  207. static inline T func10(Type x, Type y)
  208. {
  209. return T(1) - std::sin(T(2) * x) + std::cos(pi / y);
  210. }
  211. static inline T func11(Type x, Type y)
  212. {
  213. return std::sqrt(T(111.111) - std::sin(T(2) * x) + std::cos(pi / y) / T(333.333));
  214. }
  215. static inline T func12(Type x, Type y)
  216. {
  217. return ((x * x) / std::sin(T(2) * pi / y)) - x / T(2);
  218. }
  219. static inline T func13(Type x, Type y)
  220. {
  221. return (x + (std::cos(y - std::sin(T(2) / x * pi)) - std::sin(x - std::cos(T(2) * y / pi))) - y);
  222. }
  223. static inline T func14(Type x, Type y)
  224. {
  225. return clamp(T(-1), std::sin(T(2) * pi * x) + std::cos(y / T(2) * pi), + T(1));
  226. }
  227. static inline T func15(Type x, Type y)
  228. {
  229. 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)));
  230. }
  231. static inline T func16(Type x, Type y)
  232. {
  233. return (((y + (x * T(2.2))) <= (x + y + T(1.1))) ? x - y : x * y) + T(2) * pi / x;
  234. }
  235. };
  236. void pgo_primer();
  237. void perform_file_based_benchmark(const std::string& file_name, const std::size_t& rounds = 100000);
  238. int main(int argc, char* argv[])
  239. {
  240. if (argc >= 2)
  241. {
  242. const std::string file_name = argv[1];
  243. if (argc == 2)
  244. perform_file_based_benchmark(file_name);
  245. else
  246. perform_file_based_benchmark(file_name,atoi(argv[2]));
  247. return 0;
  248. }
  249. pgo_primer();
  250. double x = 0;
  251. double y = 0;
  252. exprtk::symbol_table<double> symbol_table;
  253. symbol_table.add_constants();
  254. symbol_table.add_variable("x",x);
  255. symbol_table.add_variable("y",y);
  256. std::deque<exprtk::expression<double> > compiled_expr_list;
  257. if (!load_expression(symbol_table,compiled_expr_list))
  258. {
  259. return 1;
  260. }
  261. {
  262. std::cout << "--- EXPRTK ---" << std::endl;
  263. for (std::size_t i = 0; i < compiled_expr_list.size(); ++i)
  264. {
  265. run_exprtk_benchmark(x,y,compiled_expr_list[i],global_expression_list[i]);
  266. }
  267. }
  268. {
  269. std::cout << "--- NATIVE ---" << std::endl;
  270. run_native_benchmark(x,y,native<double>::func00,global_expression_list[ 0]);
  271. run_native_benchmark(x,y,native<double>::func01,global_expression_list[ 1]);
  272. run_native_benchmark(x,y,native<double>::func02,global_expression_list[ 2]);
  273. run_native_benchmark(x,y,native<double>::func03,global_expression_list[ 3]);
  274. run_native_benchmark(x,y,native<double>::func04,global_expression_list[ 4]);
  275. run_native_benchmark(x,y,native<double>::func05,global_expression_list[ 5]);
  276. run_native_benchmark(x,y,native<double>::func06,global_expression_list[ 6]);
  277. run_native_benchmark(x,y,native<double>::func07,global_expression_list[ 7]);
  278. run_native_benchmark(x,y,native<double>::func08,global_expression_list[ 8]);
  279. run_native_benchmark(x,y,native<double>::func09,global_expression_list[ 9]);
  280. run_native_benchmark(x,y,native<double>::func10,global_expression_list[10]);
  281. run_native_benchmark(x,y,native<double>::func11,global_expression_list[11]);
  282. run_native_benchmark(x,y,native<double>::func12,global_expression_list[12]);
  283. run_native_benchmark(x,y,native<double>::func13,global_expression_list[13]);
  284. run_native_benchmark(x,y,native<double>::func14,global_expression_list[14]);
  285. run_native_benchmark(x,y,native<double>::func15,global_expression_list[15]);
  286. run_native_benchmark(x,y,native<double>::func16,global_expression_list[16]);
  287. }
  288. {
  289. std::cout << "--- PARSE ----" << std::endl;
  290. run_parse_benchmark(symbol_table);
  291. }
  292. return 0;
  293. }
  294. void pgo_primer()
  295. {
  296. exprtk::pgo_primer<double>();
  297. static const double lower_bound_x = -50.0;
  298. static const double lower_bound_y = -50.0;
  299. static const double upper_bound_x = +50.0;
  300. static const double upper_bound_y = +50.0;
  301. static const double delta = 0.07;
  302. double total = 0.0;
  303. for (double x = lower_bound_x; x <= upper_bound_x; x += delta)
  304. {
  305. for (double y = lower_bound_y; y <= upper_bound_y; y += delta)
  306. {
  307. total += native<double>::func00(x,y);
  308. total += native<double>::func01(x,y);
  309. total += native<double>::func02(x,y);
  310. total += native<double>::func03(x,y);
  311. total += native<double>::func04(x,y);
  312. total += native<double>::func05(x,y);
  313. total += native<double>::func06(x,y);
  314. total += native<double>::func07(x,y);
  315. total += native<double>::func08(x,y);
  316. total += native<double>::func09(x,y);
  317. total += native<double>::func10(x,y);
  318. total += native<double>::func11(x,y);
  319. total += native<double>::func12(x,y);
  320. total += native<double>::func13(x,y);
  321. total += native<double>::func14(x,y);
  322. total += native<double>::func15(x,y);
  323. total += native<double>::func16(x,y);
  324. }
  325. }
  326. }
  327. std::size_t load_expression_file(const std::string& file_name, std::deque<std::string>& expression_list)
  328. {
  329. std::ifstream stream(file_name.c_str());
  330. if (!stream) return 0;
  331. std::string buffer;
  332. buffer.reserve(1024);
  333. std::size_t line_count = 0;
  334. while (std::getline(stream,buffer))
  335. {
  336. if (buffer.empty())
  337. continue;
  338. else if ('#' == buffer[0])
  339. continue;
  340. ++line_count;
  341. expression_list.push_back(buffer);
  342. }
  343. return line_count;
  344. }
  345. void perform_file_based_benchmark(const std::string& file_name, const std::size_t& rounds)
  346. {
  347. std::deque<std::string> expr_str_list;
  348. if (0 == load_expression_file(file_name,expr_str_list))
  349. {
  350. std::cout << "Failed to load any expressions from: " << file_name << "\n";
  351. return;
  352. }
  353. typedef exprtk::symbol_table<double> symbol_table_t;
  354. typedef exprtk::expression<double> expression_t;
  355. typedef exprtk::parser<double> parser_t;
  356. std::deque<expression_t> expression_list;
  357. symbol_table_t symbol_table;
  358. double a = 1.1;
  359. double b = 2.2;
  360. double c = 3.3;
  361. double x = 2.123456;
  362. double y = 3.123456;
  363. double z = 4.123456;
  364. double w = 5.123456;
  365. symbol_table.add_variable("a", a);
  366. symbol_table.add_variable("b", b);
  367. symbol_table.add_variable("c", c);
  368. symbol_table.add_variable("x", x);
  369. symbol_table.add_variable("y", y);
  370. symbol_table.add_variable("z", z);
  371. symbol_table.add_variable("w", w);
  372. exprtk::polynomial<double, 1> poly01;
  373. exprtk::polynomial<double, 2> poly02;
  374. exprtk::polynomial<double, 3> poly03;
  375. exprtk::polynomial<double, 4> poly04;
  376. exprtk::polynomial<double, 5> poly05;
  377. exprtk::polynomial<double, 6> poly06;
  378. exprtk::polynomial<double, 7> poly07;
  379. exprtk::polynomial<double, 8> poly08;
  380. exprtk::polynomial<double, 9> poly09;
  381. exprtk::polynomial<double,10> poly10;
  382. exprtk::polynomial<double,11> poly11;
  383. exprtk::polynomial<double,12> poly12;
  384. symbol_table.add_function("poly01", poly01);
  385. symbol_table.add_function("poly02", poly02);
  386. symbol_table.add_function("poly03", poly03);
  387. symbol_table.add_function("poly04", poly04);
  388. symbol_table.add_function("poly05", poly05);
  389. symbol_table.add_function("poly06", poly06);
  390. symbol_table.add_function("poly07", poly07);
  391. symbol_table.add_function("poly08", poly08);
  392. symbol_table.add_function("poly09", poly09);
  393. symbol_table.add_function("poly10", poly10);
  394. symbol_table.add_function("poly11", poly11);
  395. symbol_table.add_function("poly12", poly12);
  396. static double e = exprtk::details::numeric::constant::e;
  397. symbol_table.add_variable("e", e, true);
  398. symbol_table.add_constants();
  399. {
  400. parser_t parser;
  401. for (std::size_t i = 0; i < expr_str_list.size(); ++i)
  402. {
  403. expression_t expression;
  404. expression.register_symbol_table(symbol_table);
  405. if (!parser.compile(expr_str_list[i],expression))
  406. {
  407. printf("[perform_file_based_benchmark] - Parser Error: %s\tExpression: %s\n",
  408. parser.error().c_str(),
  409. expr_str_list[i].c_str());
  410. return;
  411. }
  412. expression_list.push_back(expression);
  413. }
  414. }
  415. exprtk::timer total_timer;
  416. exprtk::timer timer;
  417. double single_eval_total_time = 0.0;
  418. total_timer.start();
  419. for (std::size_t i = 0; i < expression_list.size(); ++i)
  420. {
  421. expression_t& expression = expression_list[i];
  422. a = 1.1;
  423. b = 2.2;
  424. c = 3.3;
  425. x = 2.123456;
  426. y = 3.123456;
  427. z = 4.123456;
  428. w = 5.123456;
  429. timer.start();
  430. double sum = 0.0;
  431. for (std::size_t r = 0; r < rounds; ++r)
  432. {
  433. sum += expression.value();
  434. std::swap(a,b);
  435. std::swap(x,y);
  436. }
  437. timer.stop();
  438. printf("Expression %3d of %3d %9.3f ns\t%10d ns\t(%30.10f) '%s'\n",
  439. static_cast<int>(i + 1),
  440. static_cast<int>(expression_list.size()),
  441. (timer.time() * 1000000000.0) / (1.0 * rounds),
  442. static_cast<int>(timer.time() * 1000000000.0),
  443. sum,
  444. expr_str_list[i].c_str());
  445. fflush(stdout);
  446. single_eval_total_time += (timer.time() * 1000000000.0) / (1.0 * rounds);
  447. }
  448. total_timer.stop();
  449. printf("[*] Number Of Evals: %15.0f\n",
  450. rounds * (expression_list.size() * 1.0));
  451. printf("[*] Total Time: %9.3fsec\n",
  452. total_timer.time());
  453. printf("[*] Total Single Eval Time: %9.3fms\n",
  454. single_eval_total_time / 1000000.0);
  455. }