rhai/benches/iterations.rs

79 lines
1.7 KiB
Rust
Raw Normal View History

2020-04-13 11:26:16 +02:00
#![feature(test)]
///! Test 1,000 iterations
extern crate test;
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.run_ast(&ast).unwrap());
2020-04-13 11:26:16 +02:00
}
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)
}
}
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();
bench.iter(|| engine.eval_ast::<INT>(&ast).unwrap());
2020-04-13 17:38:10 +02:00
}
2021-11-23 11:09:41 +01:00
#[bench]
fn bench_iterations_array(bench: &mut Bencher) {
let script = r#"
let x = [];
x.pad(1000, 0);
2021-12-15 05:06:17 +01:00
for i in 0..1000 { x[i] = i % 256; }
2021-11-23 11:09:41 +01:00
"#;
let mut engine = Engine::new();
engine.set_optimization_level(OptimizationLevel::None);
let ast = engine.compile(script).unwrap();
bench.iter(|| engine.run_ast(&ast).unwrap());
}
#[bench]
fn bench_iterations_blob(bench: &mut Bencher) {
let script = r#"
let x = blob(1000, 0);
2021-12-15 05:06:17 +01:00
for i in 0..1000 { x[i] = i % 256; }
2021-11-23 11:09:41 +01:00
"#;
let mut engine = Engine::new();
engine.set_optimization_level(OptimizationLevel::None);
let ast = engine.compile(script).unwrap();
bench.iter(|| engine.run_ast(&ast).unwrap());
}