38 lines
933 B
Rust
38 lines
933 B
Rust
#![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"));
|
|
}
|