use criterion::{black_box, criterion_group, criterion_main, BenchmarkId, Criterion}; macro_rules! float_eq { ($op1:expr, $op2:expr) => { assert!(float_cmp::approx_eq!(f64, $op1, $op2)); }; } const EXPRESSIONS: [&str; 20] = [ "1 + 2", "2 * 3", "3/8.3", "4 * (1.5 - 6)", "(5 / (2 + 3 * 0.1) + 6) * 3", "(6 * ((1.5 / (2 + 3 * 0.1) + 6) * 3)) - 1.1", "(7 * (((1.5 * (0.7 - 0.33 + (6.00 * 0.05))) / (2.5 + 3 * 0.1) + 6) * 3)) - 1.1", "((8 + 7.07) * ((((1.5 - 0.001 + 0.005) * (0.7 - 0.33 + (6.00 * 0.05))) / (2.5 + 3 * 0.1) + 6) * 3)) - 1.1", "((9 + 7.07) * ((((1.5 - 0.001 + 0.005) * (0.7 - 0.33 + (6.00 * 0.05))) / (2.5 + 3 * 0.1) + 6) * 3)) - 1.1 * (1.00 * 2838.88736 * 3 / (6 - 2))", "(10 * (((2 * ((1.5 / (2 + 3 * 0.1) + (2 * (((2 * (((2 * (((2 * ((1.5 / (2 + 3 * 0.1) + (2 * (((2 * ((1.5 / (2 + 3 * 0.1) + 6) * 3)) / ((2 * (((2 * ((1.5 / (2 + 3 * 0.1) + 6) * 3)) / (2 + 3 * 0.1) + 6) * 3)) * 2 + 3 * 0.1) + 6) * 3))) * 3)) / ((2 * (((2 * ((1.5 / (2 + 3 * 0.1) + 6) * 3)) / (2 + 3 * 0.1) + 6) * 3)) * 2 + 3 * 0.1) + 6) * 3)) / (2 + 3 * 0.1) + 6) * 3)) / ((2 * (((2 * ((1.5 / (2 + 3 * 0.1) + 6) * 3)) / (2 + 3 * 0.1) + 6) * 3)) * 2 + 3 * 0.1) + 6) * 3))) * 3)) / ((2 * (((2 * ((1.5 / (2 + 3 * 0.1) + 6) * 3)) / (2 + 3 * 0.1) + 6) * 3)) * 2 + 3 * 0.1) + 6) * 3)) * (2 * (((2 * ((1.5 / (2 + 3 * 0.1) + (2 * (((2 * (((2 * (((2 * ((1.5 / (2 + 3 * 0.1) + (2 * (((2 * ((1.5 / (2 + 3 * 0.1) + 6) * 3)) / ((2 * (((2 * ((1.5 / (2 + 3 * 0.1) + 6) * 3)) / (2 + 3 * 0.1) + 6) * 3)) * 2 + 3 * 0.1) + 6) * 3))) * 3)) / ((2 * (((2 * ((1.5 / (2 + 3 * 0.1) + 6) * 3)) / (2 + 3 * 0.1) + 6) * 3)) * 2 + 3 * 0.1) + 6) * 3)) / (2 + 3 * 0.1) + 6) * 3)) / ((2 * (((2 * ((1.5 / (2 + 3 * 0.1) + 6) * 3)) / (2 + 3 * 0.1) + 6) * 3)) * 2 + 3 * 0.1) + 6) * 3))) * 3)) / ((2 * (((2 * ((1.5 / (2 + 3 * 0.1) + 6) * 3)) / (2 + 3 * 0.1) + 6) * 3)) * 2 + 3 * 0.1) + 6) * 3)) - 1.1", "11.385636 + 2.093719283674", "12.00193638 * 3.29861063", "13.29861063/8.3019836946", "14.0019869346 * (1.50128367 - 6.29861063)", "(15.01673046 / (2.0019869346 + 3.29861063 * 0.01283671) + 6) * 3.001063746", "(16.01673046 * ((1.50012360 / (2.0019869346 + 3 * 0.10128367) + 6) * 3)) - 1.10012360", "(17.001063746 * (((1.50012360 * (0.7 - 0.330012360 + (6.00 * 0.050128367))) / (2.50012360 + 3 * 0.1) + 6) * 3)) - 1.10012360", "((18 + 7.070012360) * ((((1.50012360 - 0.001 + 0.005) * (0.70128367 - 0.330012360 + (6.00 * 0.05))) / (2.50012360 + 3.0019869346 * 0.10012360) + 6) * 3)) - 1.1", "((19.01673046 + 7.070012360) * ((((1.5 - 0.001 + 0.0050012360) * (0.70128367 - 0.330012360 + (6.00 * 0.05))) / (2.5 + 3.0019869346 * 0.10012360) + 6) * 3)) - 1.10012360 * (1.00 * 2838.88736 * 3 / (6.0019869346 - 2))", "(20.00230946 * (((2.0019869346 * ((1.5 / (2.00230946 + 3 * 0.10128367) + (2.29861063 * (((2 * (((2.012930406 * (((2.00230946 * ((1.5 / (2 + 3.0019869346 * 0.1) + (2.012930406 * (((2 * ((1.5 / (2.012930406 + 3 * 0.1) + 6.029346) * 3)) / ((2 * (((2 * ((1.5 / (2.29861063 + 3.012930406 * 0.1) + 6.0019869346) * 3)) / (2.00230946 + 3 * 0.1) + 6.029346) * 3)) * 2 + 3.0019869346 * 0.1) + 6.012930406) * 3))) * 3.29861063)) / ((2.012930406 * (((2 * ((1.5 / (2 + 3.012930406 * 0.1) + 6.0019869346) * 3.012930406)) / (2.00230946 + 3 * 0.1) + 6.012930406) * 3)) * 2 + 3.012930406 * 0.1) + 6) * 3.012930406)) / (2 + 3 * 0.1) + 6.0019869346) * 3)) / ((2 * (((2.012930406 * ((1.5 / (2 + 3 * 0.1) + 6.012930406) * 3)) / (2.00230946 + 3.29861063 * 0.1) + 6.012930406) * 3)) * 2.0019869346 + 3 * 0.1) + 6.029346) * 3.012930406))) * 3)) / ((2 * (((2 * ((1.5 / (2.00230946 + 3 * 0.1) + 6.012930406) * 3.0019869346)) / (2 + 3.012930406 * 0.1) + 6) * 3)) * 2 + 3.012930406 * 0.1) + 6.0019869346) * 3)) * (2 * (((2 * ((1.5 / (2 + 3.012930406 * 0.1) + (2.0019869346 * (((2.00230946 * (((2.29861063 * (((2 * ((1.5 / (2.012930406 + 3 * 0.1) + (2 * (((2 * ((1.5 / (2.0019869346 + 3 * 0.1) + 6.029346) * 3)) / ((2.29861063 * (((2.012930406 * ((1.5 / (2.0019869346 + 3 * 0.1) + 6) * 3.012930406)) / (2.00230946 + 3.0019869346 * 0.1) + 6.012930406) * 3)) * 2 + 3.0019869346 * 0.1) + 6.029346) * 3))) * 3)) / ((2 * (((2 * ((1.5 / (2.29861063 + 3.0019869346 * 0.1) + 6.001063746) * 3)) / (2.0019869346 + 3 * 0.10128367) + 6) * 3)) * 2.0019869346 + 3.001063746 * 0.1) + 6.029346) * 3)) / (2.0019869346 + 3 * 0.10128367) + 6) * 3)) / ((2.0019869346 * (((2.001063746 * ((1.5 / (2 + 3.29861063 * 0.10128367) + 6.029346) * 3)) / (2.0019869346 + 3 * 0.1) + 6.001063746) * 3)) * 2 + 3.0019869346 * 0.10128367) + 6) * 3))) * 3)) / ((2.001063746 * (((2 * ((1.50128367 / (2.29861063 + 3 * 0.1) + 6.0019869346) * 3)) / (2.00230946 + 3 * 0.10128367) + 6.029346) * 3)) * 2.001063746 + 3.29861063 * 0.1) + 6) * 3.29861063)) - 1.10128367", ]; fn bench_mexe(c: &mut Criterion) { for expr in EXPRESSIONS.iter() { dbg!(mexe::eval(expr)); float_eq!(mexe::eval(expr).unwrap(), meval::eval_str(expr).unwrap()); let bench_id = format!("bench_mexe {}", expr); c.bench_function(&bench_id, |b| b.iter(|| mexe::eval(black_box(expr)))); } } fn bench_cmp(c: &mut Criterion) { let mut group = c.benchmark_group("bench_eval"); for expr in EXPRESSIONS.iter() { dbg!(mexe::eval(expr)); dbg!(meval::eval_str(expr)); dbg!(fasteval::ez_eval(expr, &mut fasteval::EmptyNamespace)); dbg!(evalexpr::eval(expr)); float_eq!(mexe::eval(expr).unwrap(), meval::eval_str(expr).unwrap()); group.bench_with_input( BenchmarkId::new("bench_cmp mexe", expr), expr, |b, &expr| { b.iter(|| mexe::eval(expr)); }, ); group.bench_with_input( BenchmarkId::new("bench_cmp fasteval", expr), expr, |b, &expr| { b.iter(|| fasteval::ez_eval(expr, &mut fasteval::EmptyNamespace)); }, ); group.bench_with_input( BenchmarkId::new("bench_cmp meval", expr), expr, |b, &expr| { b.iter(|| meval::eval_str(expr)); }, ); group.bench_with_input( BenchmarkId::new("bench_cmp evalexpr", expr), expr, |b, &expr| { b.iter(|| evalexpr::eval(expr)); }, ); } group.finish(); } criterion_group!(benches, bench_cmp, bench_mexe); criterion_main!(benches);