|
| 1 | +#include "BenchTinyExpr.h" |
| 2 | + |
| 3 | +#include <cmath> |
| 4 | + |
| 5 | +#include "tinyexpr/tinyexpr.h" |
| 6 | + |
| 7 | + |
| 8 | +//------------------------------------------------------------------------------------------------- |
| 9 | +BenchTinyExpr::BenchTinyExpr() |
| 10 | +: Benchmark() |
| 11 | +{ |
| 12 | + m_sName = "TinyExpr"; |
| 13 | +} |
| 14 | + |
| 15 | +//------------------------------------------------------------------------------------------------- |
| 16 | +double BenchTinyExpr::DoBenchmark(const std::string& sExpr, long iCount) |
| 17 | +{ |
| 18 | + double a = 1.1; |
| 19 | + double b = 2.2; |
| 20 | + double c = 3.3; |
| 21 | + double x = 2.123456; |
| 22 | + double y = 3.123456; |
| 23 | + double z = 4.123456; |
| 24 | + double w = 5.123456; |
| 25 | + double e = 2.718281828459045235360; |
| 26 | + double p = 3.141592653589793238462; |
| 27 | + |
| 28 | + te_variable vars[] = |
| 29 | + { |
| 30 | + {"a", &a}, |
| 31 | + {"b", &b}, |
| 32 | + {"c", &c}, |
| 33 | + {"x", &x}, |
| 34 | + {"y", &y}, |
| 35 | + {"z", &z}, |
| 36 | + {"w", &w}, |
| 37 | + {"e", &e}, |
| 38 | + {"pi",&p} |
| 39 | + }; |
| 40 | + |
| 41 | + int error = 0; |
| 42 | + te_expr* expression = reinterpret_cast<te_expr*>(0); |
| 43 | + |
| 44 | + expression = te_compile(sExpr.c_str(), vars, sizeof(vars) / sizeof(te_variable), &error); |
| 45 | + |
| 46 | + if ((0 == expression) || error) |
| 47 | + { |
| 48 | + StopTimer(std::numeric_limits<double>::quiet_NaN(), |
| 49 | + std::numeric_limits<double>::quiet_NaN(), |
| 50 | + 1); |
| 51 | + |
| 52 | + return std::numeric_limits<double>::quiet_NaN(); |
| 53 | + } |
| 54 | + |
| 55 | + //Prime the I and D caches for the expression |
| 56 | + { |
| 57 | + double d0 = 0.0; |
| 58 | + double d1 = 0.0; |
| 59 | + |
| 60 | + for (std::size_t i = 0; i < priming_rounds; ++i) |
| 61 | + { |
| 62 | + if (i & 1) |
| 63 | + d0 += te_eval(expression); |
| 64 | + else |
| 65 | + d1 += te_eval(expression); |
| 66 | + } |
| 67 | + |
| 68 | + if ( |
| 69 | + (d0 == std::numeric_limits<double>::infinity()) && |
| 70 | + (d1 == std::numeric_limits<double>::infinity()) |
| 71 | + ) |
| 72 | + { |
| 73 | + printf("\n"); |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + // Perform benchmark then return results |
| 78 | + double fRes = 0; |
| 79 | + double fSum = 0; |
| 80 | + |
| 81 | + fRes = te_eval(expression); |
| 82 | + |
| 83 | + StartTimer(); |
| 84 | + |
| 85 | + for (int j = 0; j < iCount; ++j) |
| 86 | + { |
| 87 | + fSum += te_eval(expression); |
| 88 | + std::swap(a,b); |
| 89 | + std::swap(x,y); |
| 90 | + } |
| 91 | + |
| 92 | + StopTimer(fRes, fSum, iCount); |
| 93 | + |
| 94 | + te_free(expression); |
| 95 | + |
| 96 | + return m_fTime1; |
| 97 | +} |
0 commit comments