benchmarks

This commit is contained in:
2018-05-23 14:40:33 +02:00
parent a652dc1a17
commit 133291a6d4

37
benches/brainfuck.rs Normal file
View File

@@ -0,0 +1,37 @@
#![feature(test)]
extern crate rustijvm;
extern crate test;
use std::fs::File;
use std::rc::Rc;
use std::sync::Mutex;
use std::io::{Cursor, Seek, SeekFrom, Read};
use test::Bencher;
fn run_bfi(file: &str) -> String {
let file = File::open(file).expect("Missing bf file");
let rc = Rc::new(Mutex::new(Cursor::new(Vec::new())));
let mut machine = rustijvm::Machine::new_from_file("files/bonus/bfi.ijvm").unwrap();
machine.set_output(rc.clone());
machine.set_input(Box::new(file));
machine.run().unwrap();
let mut out = rc.lock().unwrap();
let mut string = String::new();
out.seek(SeekFrom::Start(0)).unwrap();
out.read_to_string(&mut string).unwrap();
string
}
#[bench]
fn bench_bf_hello_world(b: &mut Bencher) {
b.iter(|| run_bfi("files/bonus/brainfuck/hello_world.bf"));
}
#[bench]
fn bench_bf_dank(b: &mut Bencher) {
b.iter(|| run_bfi("files/bonus/brainfuck/dank.bf"));
}