Skip to content

Commit

Permalink
feat: add formatter bench
Browse files Browse the repository at this point in the history
  • Loading branch information
Yag000 committed Aug 10, 2023
1 parent 93aad71 commit ddc1b7c
Show file tree
Hide file tree
Showing 3 changed files with 208 additions and 0 deletions.
7 changes: 7 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@ clap_derive = "4.3.2"
criterion = "0.5.1"
chrono = "0.4.26"


[[bench]]
name = "formatter_bench"
harness = false

[[bench]]
name = "fibonacci_bench"
harness = false
Expand All @@ -43,3 +48,5 @@ harness = false
name = "array_bench"
harness = false



201 changes: 201 additions & 0 deletions benches/formatter_bench.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,201 @@
use criterion::{black_box, criterion_group, criterion_main, Criterion};

use formatter::formatter::Formatter;

pub fn long_formatter_benchmark(c: &mut Criterion) {
let input = r#"
let fibonacci_it = fn (x) {
if (x < 2) {
return x;
}
let iter = fn (i, table) {
if (i > x) {
return last(table);
} else {
let new_table = push(table, table[i - 1] + table[i - 2]);
return iter(i + 1, new_table);
}
};
return iter(2, [0, 1]);
};
let fib = fibonacci_it(20);
puts (fib);
let fibonacci = fn (x) {
if (x < 2) {
x
} else {
fibonacci(x - 1) + fibonacci(x - 2)
}
};
puts(fibonacci(3));
let filter = fn (arr, f) {
let iter = fn (arr, accumulated) {
if (len(arr) == 0) {
accumulated
} else {
let head = first(arr);
let tail = rest(arr);
if (f(head)) {
iter(tail, push(accumulated, head))
} else {
iter(tail, accumulated)
}
}
};
iter(arr, [])
};
let a = [1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 100];
let is_even = fn (x) {
x / 2 * 2 == x
};
filter(a, is_even);
let foldl = fn (arr, initial , f) {
let iter = fn (arr, result) {
if (len(arr) == 0) {
result;
} else {
iter(rest(arr), f(result, first(arr)))
}
};
iter(arr, initial)
};
let a = [1, 2, 3, 4];
let sum = fn (x, y) {
x + y
};
foldl(a, 0, sum);
let foldr = fn(arr, initial, f) {
let iter = fn(arr, result) {
if (len(arr) == 0) {
result
} else {
iter(rest(arr), f(first(arr), result));
}
};
iter(arr, initial);
};
let a = [1, 2, 3, 4];
let sum = fn(x, y) { x + y };
foldr(a, 0, sum);
let input = r;
"
let map = fn(arr, f) {
let iter = fn(arr, accumulated) {
if (len(arr) == 0) {
accumulated
} else {
iter(rest(arr), push(accumulated, f(first(arr))));
}
};
iter(arr, []);
};
let a = [1, 2, 3, 4];
let double = fn(x) { x * 2 };
map(a, double);
";
~/Projects/monkey-rs 3-Formatter *2 !1 ?1 ❯ cat monkey_examples/* 17:00:36
let fibonacci_it = fn (x) {
if (x < 2) {
return x;
}
let iter = fn (i, table) {
if (i > x) {
return last(table);
} else {
let new_table = push(table, table[i - 1] + table[i - 2]);
return iter(i + 1, new_table);
}
};
return iter(2, [0, 1]);
};
let fib = fibonacci_it(20);
puts(fib);
let fibonacci = fn (x) {
if (x < 2) {
x
} else {
fibonacci(x - 1) + fibonacci(x - 2)
}
};
puts(fibonacci(30));
let filter = fn (arr, f) {
let iter = fn (arr, accumulated) {
if (len(arr) == 0) {
accumulated
} else {
let head = first(arr);
let tail = rest(arr);
if (f(head)) {
iter(tail, push(accumulated, head))
} else {
iter(tail, accumulated)
}
}
};
iter(arr, [])
};
let a = [1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 100];
let is_even = fn (x) {
x / 2 * 2 == x
};
filter(a, is_even);
let foldl = fn (arr, initial, f) {
let iter = fn (arr, result) {
if (len(arr) == 0) {
result
} else {
iter(rest(arr), f(result, first(arr)))
}
};
iter(arr, initial)
};
let a = [1, 2, 3, 4];
let sum = fn (x, y) {
x + y
};
foldl(a, 0, sum);
let foldr = fn(arr, initial, f) {
let iter = fn(arr, result) {
if (len(arr) == 0) {
result
} else {
iter(rest(arr), f(first(arr), result));
}
};
iter(arr, initial);
};
let a = [1, 2, 3, 4];
let sum = fn(x, y) { x + y };
foldr(a, 0, sum);
let map = fn(arr, f) {let iter = fn(arr, accumulated) {
if (len(arr) == 0) {
accumulated
} else {
iter(rest(arr), push(accumulated, f(first(arr))));
}
};
iter(arr, []);
};
let a = [1, 2, 3, 4];
let double = fn(x) { x * 2 };
map(a, double);
"#;

c.bench_function("Long format", |b| {
b.iter(|| Formatter::format(black_box(input)))
});
}

criterion_group!(benches, long_formatter_benchmark);
criterion_main!(benches);
Empty file removed crates/formatter/src/bin.rs
Empty file.

0 comments on commit ddc1b7c

Please sign in to comment.