1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
|
/*
* TINYEXPR - Tiny recursive descent parser and evaluation engine in C
*
* Copyright (c) 2015, 2016 Lewis Van Winkle
*
* http://CodePlea.com
*
* This software is provided 'as-is', without any express or implied
* warranty. In no event will the authors be held liable for any damages
* arising from the use of this software.
*
* Permission is granted to anyone to use this software for any purpose,
* including commercial applications, and to alter it and redistribute it
* freely, subject to the following restrictions:
*
* 1. The origin of this software must not be misrepresented; you must not
* claim that you wrote the original software. If you use this software
* in a product, an acknowledgement in the product documentation would be
* appreciated but is not required.
* 2. Altered source versions must be plainly marked as such, and must not be
* misrepresented as being the original software.
* 3. This notice may not be removed or altered from any source distribution.
*/
#include <stdio.h>
#include <time.h>
#include <math.h>
#include "tinyexpr.h"
#define loops 10000
typedef double (*function1)(double);
void bench(const char *expr, function1 func) {
int i, j;
volatile double d;
double tmp;
clock_t start;
te_variable lk = {"a", &tmp};
printf("Expression: %s\n", expr);
printf("native ");
start = clock();
d = 0;
for (j = 0; j < loops; ++j)
for (i = 0; i < loops; ++i) {
tmp = i;
d += func(tmp);
}
const int nelapsed = (clock() - start) * 1000 / CLOCKS_PER_SEC;
/*Million floats per second input.*/
printf(" %.5g", d);
if (nelapsed)
printf("\t%5dms\t%5dmfps\n", nelapsed, loops * loops / nelapsed / 1000);
else
printf("\tinf\n");
printf("interp ");
te_expr *n = te_compile(expr, &lk, 1, 0);
start = clock();
d = 0;
for (j = 0; j < loops; ++j)
for (i = 0; i < loops; ++i) {
tmp = i;
d += te_eval(n);
}
const int eelapsed = (clock() - start) * 1000 / CLOCKS_PER_SEC;
te_free(n);
/*Million floats per second input.*/
printf(" %.5g", d);
if (eelapsed)
printf("\t%5dms\t%5dmfps\n", eelapsed, loops * loops / eelapsed / 1000);
else
printf("\tinf\n");
printf("%.2f%% longer\n", (((double)eelapsed / nelapsed) - 1.0) * 100.0);
printf("\n");
}
double a5(double a) {
return a+5;
}
double a52(double a) {
return (a+5)*2;
}
double a10(double a) {
return a+(5*2);
}
double as(double a) {
return sqrt(pow(a, 1.5) + pow(a, 2.5));
}
double al(double a) {
return (1/(a+1)+2/(a+2)+3/(a+3));
}
int main(int argc, char *argv[])
{
bench("sqrt(a^1.5+a^2.5)", as);
bench("a+5", a5);
bench("a+(5*2)", a10);
bench("(a+5)*2", a52);
bench("(1/(a+1)+2/(a+2)+3/(a+3))", al);
return 0;
}
|