2020-04-19 12:33:09 +02:00
|
|
|
#![feature(test)]
|
|
|
|
|
|
|
|
///! Test evaluating expressions
|
|
|
|
extern crate test;
|
|
|
|
|
|
|
|
use rhai::{Engine, OptimizationLevel};
|
|
|
|
use test::Bencher;
|
|
|
|
|
|
|
|
#[bench]
|
|
|
|
fn bench_eval_array_small_get(bench: &mut Bencher) {
|
|
|
|
let script = "let x = [1, 2, 3, 4, 5]; x[3]";
|
|
|
|
|
|
|
|
let mut engine = Engine::new();
|
|
|
|
engine.set_optimization_level(OptimizationLevel::None);
|
|
|
|
|
|
|
|
let ast = engine.compile(script).unwrap();
|
|
|
|
|
2021-08-06 08:46:27 +02:00
|
|
|
bench.iter(|| engine.run_ast(&ast).unwrap());
|
2020-04-19 12:33:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#[bench]
|
|
|
|
fn bench_eval_array_small_set(bench: &mut Bencher) {
|
|
|
|
let script = "let x = [1, 2, 3, 4, 5]; x[3] = 42;";
|
|
|
|
|
|
|
|
let mut engine = Engine::new();
|
|
|
|
engine.set_optimization_level(OptimizationLevel::None);
|
|
|
|
|
|
|
|
let ast = engine.compile(script).unwrap();
|
|
|
|
|
2021-08-06 08:46:27 +02:00
|
|
|
bench.iter(|| engine.run_ast(&ast).unwrap());
|
2020-04-19 12:33:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#[bench]
|
|
|
|
fn bench_eval_array_large_get(bench: &mut Bencher) {
|
|
|
|
let script = r#"let x = [ 1, 2.345, "hello", true,
|
|
|
|
[ 1, 2, 3, [ "hey", [ "deeply", "nested" ], "jude" ] ]
|
|
|
|
];
|
|
|
|
x[4][3][1][1]
|
|
|
|
"#;
|
|
|
|
|
|
|
|
let mut engine = Engine::new();
|
|
|
|
engine.set_optimization_level(OptimizationLevel::None);
|
|
|
|
|
|
|
|
let ast = engine.compile(script).unwrap();
|
|
|
|
|
2021-08-06 08:46:27 +02:00
|
|
|
bench.iter(|| engine.run_ast(&ast).unwrap());
|
2020-04-19 12:33:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#[bench]
|
|
|
|
fn bench_eval_array_large_set(bench: &mut Bencher) {
|
|
|
|
let script = r#"let x = [ 1, 2.345, "hello", true,
|
|
|
|
[ 1, 2, 3, [ "hey", [ "deeply", "nested" ], "jude" ] ]
|
|
|
|
];
|
2020-04-26 12:37:41 +02:00
|
|
|
x[4][3][1][1] = 42
|
2020-04-19 12:33:09 +02:00
|
|
|
"#;
|
|
|
|
|
|
|
|
let mut engine = Engine::new();
|
|
|
|
engine.set_optimization_level(OptimizationLevel::None);
|
|
|
|
|
|
|
|
let ast = engine.compile(script).unwrap();
|
|
|
|
|
2021-08-06 08:46:27 +02:00
|
|
|
bench.iter(|| engine.run_ast(&ast).unwrap());
|
2020-04-19 12:33:09 +02:00
|
|
|
}
|
2020-05-31 17:44:49 +02:00
|
|
|
|
|
|
|
#[bench]
|
|
|
|
fn bench_eval_array_loop(bench: &mut Bencher) {
|
2021-12-30 15:53:59 +01:00
|
|
|
let script = "
|
2020-05-31 17:44:49 +02:00
|
|
|
let list = [];
|
|
|
|
|
2021-12-15 05:06:17 +01:00
|
|
|
for i in 0..10_000 {
|
2020-05-31 17:44:49 +02:00
|
|
|
list.push(i);
|
|
|
|
}
|
|
|
|
|
|
|
|
let sum = 0;
|
|
|
|
|
|
|
|
for i in list {
|
|
|
|
sum += i;
|
|
|
|
}
|
2021-12-30 15:53:59 +01:00
|
|
|
";
|
2020-05-31 17:44:49 +02:00
|
|
|
|
|
|
|
let mut engine = Engine::new();
|
|
|
|
engine.set_optimization_level(OptimizationLevel::None);
|
|
|
|
|
|
|
|
let ast = engine.compile(script).unwrap();
|
|
|
|
|
2021-08-06 08:46:27 +02:00
|
|
|
bench.iter(|| engine.run_ast(&ast).unwrap());
|
2020-05-31 17:44:49 +02:00
|
|
|
}
|