Skip to content

Commit 03e236e

Browse files
committed
More benchmarks, update readme
1 parent 53f4a6c commit 03e236e

File tree

4 files changed

+56
-24
lines changed

4 files changed

+56
-24
lines changed

.github/workflows/bench.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@ env:
1010
CARGO_TERM_COLOR: always
1111

1212
jobs:
13-
build:
13+
benchmarks:
1414
runs-on: ubuntu-latest
1515
steps:
1616
- uses: actions/checkout@v2
1717
- name: Run benchmarks
18-
run: cargo bench
18+
run: cargo bench -- bench_cmp

.github/workflows/unit.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
name: Unit Test
1+
name: Unit Tests
22

33
on:
44
push:
@@ -10,7 +10,7 @@ env:
1010
CARGO_TERM_COLOR: always
1111

1212
jobs:
13-
build:
13+
unit_tests:
1414
runs-on: ubuntu-latest
1515
steps:
1616
- uses: actions/checkout@v2

README.md

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,24 @@
1010
let forty_six = eval("(5 * 8) + 6").unwrap();
1111
let two = eval("1 + 1").unwrap();
1212
println!("{} & {}", forty_six, two);
13+
14+
assert_eq!(forty_six, 46.0);
15+
assert_eq!(two, 2.0);
1316
}
1417

18+
Note: the above `assert_eq`s work, but for float comparison in general use a
19+
crate such as `float-cmp`.
20+
1521
## Why?
1622

1723
If you need to evaluate simple arithmetic expressions, this crate offers a fast
1824
and lightweight solution.
1925

20-
It's about 4-10x faster than `meval` and about 2x faster than `fasteval`, but
21-
there are still optimisations to come, which will make it even faster. Note that
22-
those crates do much more than `mexe`. Our focus on a very small problem makes
23-
it easier for us to ship a fast and lean library.
26+
In our [current benchmarks](https://github.com/yds12/mexe/actions/workflows/bench.yml),
27+
it's about 4-10x faster than `meval` and about 2x
28+
faster than `fasteval`, but there are still optimisations to come, which will
29+
make it even faster. Note that those crates do much more than `mexe`. Our focus
30+
on a very small problem makes it easier for us to ship a fast and lean library.
2431

2532
## Includes
2633

@@ -41,12 +48,25 @@ it easier for us to ship a fast and lean library.
4148
- We can assume the input is ASCII, and throw an error otherwise
4249
- Thoroughly tested
4350
- Maybe try to make it no-std
44-
- Having fun
51+
52+
## Run Tests and Benchmarks
53+
54+
Unit tests:
55+
56+
cargo test
57+
58+
Benchmarks:
59+
60+
cargo bench -- bench_cmp # comparison with other crates
61+
cargo bench -- bench_mexe # only mexe
62+
63+
Fuzz tests have been ran with [cargo-fuzz](https://github.com/rust-fuzz/cargo-fuzz).
4564

4665
## Similar Projects
4766

67+
- [meval](https://crates.io/crates/meval)
68+
- [fasteval](https://crates.io/crates/fasteval)
4869
- [pmalmgren/rust-calculator](https://github.com/pmalmgren/rust-calculator)
49-
- [rekka/meval-rs](https://github.com/rekka/meval-rs)
5070
- [adriaN/simple_rust_parser](https://github.com/adrianN/simple_rust_parser)
5171

5272
## Grammar

benches/benchmarks.rs

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,54 @@
1-
use criterion::{criterion_group, criterion_main, BenchmarkId, Criterion};
1+
use criterion::{criterion_group, criterion_main, BenchmarkId, Criterion, black_box};
22

33
macro_rules! float_eq {
44
($op1:expr, $op2:expr) => {
55
assert!(float_cmp::approx_eq!(f64, $op1, $op2));
66
};
77
}
88

9-
fn bench_eval(c: &mut Criterion) {
10-
let expressions = [
9+
const EXPRESSIONS: [&str; 10] = [
1110
"1 + 2",
12-
"4 * 3",
13-
"12.837/8.3",
14-
"2 * (1.5 - 6)",
15-
"(1.5 / (2 + 3 * 0.1) + 6) * 3",
16-
"(2 * ((1.5 / (2 + 3 * 0.1) + 6) * 3)) - 1.1",
17-
"(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)) * (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+
"2 * 3",
12+
"3/8.3",
13+
"4 * (1.5 - 6)",
14+
"(5 / (2 + 3 * 0.1) + 6) * 3",
15+
"(6 * ((1.5 / (2 + 3 * 0.1) + 6) * 3)) - 1.1",
16+
"(7 * (((1.5 * (0.7 - 0.33 + (6.00 * 0.05))) / (2.5 + 3 * 0.1) + 6) * 3)) - 1.1",
17+
"((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",
18+
"((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))",
19+
"(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",
1820
];
1921

22+
fn bench_mexe(c: &mut Criterion) {
23+
for expr in EXPRESSIONS.iter() {
24+
dbg!(mexe::eval(expr));
25+
float_eq!(mexe::eval(expr).unwrap(), meval::eval_str(expr).unwrap());
26+
let bench_id = format!("bench_mexe {}", expr);
27+
c.bench_function(&bench_id, |b| b.iter(|| mexe::eval(black_box(expr))));
28+
}
29+
}
30+
31+
fn bench_cmp(c: &mut Criterion) {
2032
let mut group = c.benchmark_group("bench_eval");
2133

22-
for expr in expressions.iter() {
34+
for expr in EXPRESSIONS.iter() {
2335
dbg!(mexe::eval(expr));
2436
dbg!(meval::eval_str(expr));
2537
dbg!(fasteval::ez_eval(expr, &mut fasteval::EmptyNamespace));
2638
float_eq!(mexe::eval(expr).unwrap(), meval::eval_str(expr).unwrap());
2739

28-
group.bench_with_input(BenchmarkId::new("mexe", expr), expr, |b, &expr| {
40+
group.bench_with_input(BenchmarkId::new("bench_cmp mexe", expr), expr, |b, &expr| {
2941
b.iter(|| mexe::eval(expr));
3042
});
31-
group.bench_with_input(BenchmarkId::new("meval", expr), expr, |b, &expr| {
43+
group.bench_with_input(BenchmarkId::new("bench_cmp meval", expr), expr, |b, &expr| {
3244
b.iter(|| meval::eval_str(expr));
3345
});
34-
group.bench_with_input(BenchmarkId::new("fasteval", expr), expr, |b, &expr| {
46+
group.bench_with_input(BenchmarkId::new("bench_cmp fasteval", expr), expr, |b, &expr| {
3547
b.iter(|| fasteval::ez_eval(expr, &mut fasteval::EmptyNamespace));
3648
});
3749
}
3850
group.finish();
3951
}
4052

41-
criterion_group!(benches, bench_eval);
53+
criterion_group!(benches, bench_cmp, bench_mexe);
4254
criterion_main!(benches);

0 commit comments

Comments
 (0)