2020-04-13 11:26:16 +02:00
|
|
|
#![feature(test)]
|
|
|
|
|
|
|
|
///! Test 1,000 iterations
|
|
|
|
extern crate test;
|
|
|
|
|
2020-04-20 18:11:25 +02:00
|
|
|
use rhai::{Engine, OptimizationLevel, INT};
|
2020-04-13 11:26:16 +02:00
|
|
|
use test::Bencher;
|
|
|
|
|
|
|
|
#[bench]
|
|
|
|
fn bench_iterations_1000(bench: &mut Bencher) {
|
|
|
|
let script = r#"
|
|
|
|
let x = 1_000;
|
|
|
|
|
|
|
|
while x > 0 {
|
2020-05-25 14:14:31 +02:00
|
|
|
x -= 1;
|
2020-04-13 11:26:16 +02:00
|
|
|
}
|
|
|
|
"#;
|
|
|
|
|
|
|
|
let mut engine = Engine::new();
|
|
|
|
engine.set_optimization_level(OptimizationLevel::None);
|
|
|
|
|
|
|
|
let ast = engine.compile(script).unwrap();
|
|
|
|
|
|
|
|
bench.iter(|| engine.consume_ast(&ast).unwrap());
|
|
|
|
}
|
2020-04-13 17:38:10 +02:00
|
|
|
|
|
|
|
#[bench]
|
|
|
|
fn bench_iterations_fibonacci(bench: &mut Bencher) {
|
|
|
|
let script = r#"
|
|
|
|
fn fibonacci(n) {
|
|
|
|
if n < 2 {
|
|
|
|
n
|
|
|
|
} else {
|
|
|
|
fibonacci(n-1) + fibonacci(n-2)
|
|
|
|
}
|
|
|
|
}
|
2020-04-20 18:11:25 +02:00
|
|
|
|
|
|
|
fibonacci(20)
|
2020-04-13 17:38:10 +02:00
|
|
|
"#;
|
|
|
|
|
|
|
|
let mut engine = Engine::new();
|
|
|
|
engine.set_optimization_level(OptimizationLevel::None);
|
|
|
|
|
|
|
|
let ast = engine.compile(script).unwrap();
|
|
|
|
|
2020-04-20 18:11:25 +02:00
|
|
|
bench.iter(|| engine.eval_ast::<INT>(&ast).unwrap());
|
2020-04-13 17:38:10 +02:00
|
|
|
}
|