mirror of
https://github.com/Noratrieb/brainfuck.git
synced 2026-01-16 06:15:02 +01:00
bench setup that works
This commit is contained in:
parent
7b88c99039
commit
e82b14b09a
11 changed files with 302 additions and 19 deletions
2
rust2/benches/bench.bf
Normal file
2
rust2/benches/bench.bf
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
++++++++[->-[->-[->-[-]<]<]<]
|
||||
>++++++++[<++++++++++>-]<[>+>+<<-]>-.>-----.>
|
||||
7
rust2/benches/fizzbuzz.bf
Normal file
7
rust2/benches/fizzbuzz.bf
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
++++++++++[>++++++++++<-]>>++++++++++>->>>>>>>>>>>>>>>>-->+++++++[->++\n++++++++<]>[->+>+>+>+<<<<]+++>>+++>>>++++++++[-<
|
||||
++++<++++<++++>>>]++++\n+[-<++++<++++>>]>>-->++++++[->+++++++++++<]>[->+>+>+>+<<<<]+++++>>+>++\n++++>++++++>++++++++[-<+
|
||||
+++<++++<++++>>>]++++++[-<+++<+++<+++>>>]>>-->\n---+[-<+]-<[+[->+]-<<->>>+>[-]++[-->++]-->+++[---++[--<++]---->>-<+>[+\n
|
||||
+++[----<++++]--[>]++[-->++]--<]>++[--+[-<+]->>[-]+++++[---->++++]-->[\n->+<]>>[.>]++[-->++]]-->+++]---+[-<+]->>-[+>>>+[
|
||||
-<+]->>>++++++++++<<[-\n>+>-[>+>>]>[+[-<+>]>+>>]<<<<<<]>>[-]>>>++++++++++<[->-[>+>>]>[+[-<+>]>\n+>>]<<<<<]>[-]>>[>++++++
|
||||
[-<++++++++>]<.<<+>+>[-]]<[<[->-<]++++++[->+++\n+++++<]>.[-]]<<++++++[-<++++++++>]<.[-]<<[-<+>]+[-<+]->>]+[-]<<<.>>>+[\n
|
||||
-<+]-<<]
|
||||
1
rust2/benches/hello.bf
Normal file
1
rust2/benches/hello.bf
Normal file
|
|
@ -0,0 +1 @@
|
|||
++++++++[>++++[>++>+++>+++>+<<<<-]>+>+>->>+[<]<-]>>.>---.+++++++..+++.>>.<-.<.+++.------.--------.>>+.>++.
|
||||
4
rust2/benches/loopremove.bf
Normal file
4
rust2/benches/loopremove.bf
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
>+++++[-]++++++++++[[-->+++++++++>+++++++++>+++++++++<<<<->]]>.>.>.
|
||||
<[-]<[-]<[-]
|
||||
>>>>+ >>>>> ++++++++++ <<<< <<<<<
|
||||
+[[>>>>]]+[[++--[+++>>]]>.][-]
|
||||
|
|
@ -1,16 +1,55 @@
|
|||
use criterion::{black_box, criterion_group, criterion_main, Criterion};
|
||||
use bumpalo::Bump;
|
||||
use criterion::{criterion_group, criterion_main, Criterion};
|
||||
use std::fs;
|
||||
use std::io::{Read, Write};
|
||||
use std::path::PathBuf;
|
||||
|
||||
fn fibonacci(n: u64) -> u64 {
|
||||
match n {
|
||||
0 => 1,
|
||||
1 => 1,
|
||||
n => fibonacci(n - 1) + fibonacci(n - 2),
|
||||
struct MockReadWrite;
|
||||
|
||||
impl Read for MockReadWrite {
|
||||
fn read(&mut self, buf: &mut [u8]) -> std::io::Result<usize> {
|
||||
buf.fill(b'A');
|
||||
Ok(buf.len())
|
||||
}
|
||||
}
|
||||
|
||||
fn criterion_benchmark(c: &mut Criterion) {
|
||||
c.bench_function("fib 20", |b| b.iter(|| fibonacci(black_box(20))));
|
||||
impl Write for MockReadWrite {
|
||||
fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> {
|
||||
Ok(buf.len())
|
||||
}
|
||||
|
||||
fn flush(&mut self) -> std::io::Result<()> {
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
criterion_group!(benches, criterion_benchmark);
|
||||
fn get_bf(path_from_bench: impl AsRef<str>) -> String {
|
||||
let file = PathBuf::from(file!())
|
||||
.parent()
|
||||
.unwrap()
|
||||
.join(path_from_bench.as_ref());
|
||||
|
||||
fs::read_to_string(file).unwrap()
|
||||
}
|
||||
|
||||
fn run_bf_bench(bf: &str) {
|
||||
let bump = Bump::new();
|
||||
let parsed = brainfuck::parse::parse(&bump, bf.bytes()).unwrap();
|
||||
let optimized = brainfuck::opts::optimize(&bump, &parsed);
|
||||
brainfuck::ir_interpreter::run(&optimized, MockReadWrite, MockReadWrite);
|
||||
}
|
||||
|
||||
fn optimized(c: &mut Criterion) {
|
||||
let fizzbuzz = get_bf("fizzbuzz.bf");
|
||||
let hello_world = get_bf("hello.bf");
|
||||
let bench = get_bf("bench.bf");
|
||||
let loopremove = get_bf("loopremove.bf");
|
||||
|
||||
c.bench_function("fizzbuzz", |b| b.iter(|| run_bf_bench(&fizzbuzz)));
|
||||
c.bench_function("hello_world", |b| b.iter(|| run_bf_bench(&hello_world)));
|
||||
c.bench_function("bench", |b| b.iter(|| run_bf_bench(&bench)));
|
||||
c.bench_function("loopremove", |b| b.iter(|| run_bf_bench(&loopremove)));
|
||||
}
|
||||
|
||||
criterion_group!(benches, optimized);
|
||||
criterion_main!(benches);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue