rhai/benches/eval_expression.rs

221 lines
5.4 KiB
Rust
Raw Normal View History

2020-04-13 11:26:16 +02:00
#![feature(test)]
///! Test evaluating expressions
extern crate test;
use rhai::{Engine, OptimizationLevel};
use test::Bencher;
#[bench]
fn bench_eval_expression_single(bench: &mut Bencher) {
let script = "1";
let mut engine = Engine::new();
engine.set_optimization_level(OptimizationLevel::None);
let ast = engine.compile_expression(script).unwrap();
bench.iter(|| engine.run_ast(&ast).unwrap());
2020-04-13 11:26:16 +02:00
}
#[bench]
fn bench_eval_expression_number_literal(bench: &mut Bencher) {
let script = "2 > 1";
let mut engine = Engine::new();
engine.set_optimization_level(OptimizationLevel::None);
let ast = engine.compile_expression(script).unwrap();
bench.iter(|| engine.run_ast(&ast).unwrap());
2020-04-13 11:26:16 +02:00
}
#[bench]
fn bench_eval_expression_number_operators(bench: &mut Bencher) {
let script = "2 + 2 == 4";
let mut engine = Engine::new();
engine.set_optimization_level(OptimizationLevel::None);
let ast = engine.compile_expression(script).unwrap();
bench.iter(|| engine.run_ast(&ast).unwrap());
2020-04-13 11:26:16 +02:00
}
2020-05-20 05:12:35 +02:00
#[bench]
fn bench_eval_expression_optimized_simple(bench: &mut Bencher) {
let script = r#"
2 > 1 &&
"something" != "nothing" ||
"2014-01-20" < "Wed Jul 8 23:07:35 MDT 2015" &&
2020-07-29 10:09:48 +02:00
[array, has, spaces].len <= #{prop:name}.len &&
2020-05-20 05:12:35 +02:00
modifierTest + 1000 / 2 > (80 * 100 % 2)
"#;
let mut engine = Engine::new();
engine.set_optimization_level(OptimizationLevel::Simple);
let ast = engine.compile_expression(script).unwrap();
bench.iter(|| engine.run_ast(&ast).unwrap());
2020-05-20 05:12:35 +02:00
}
#[bench]
fn bench_eval_expression_optimized_full(bench: &mut Bencher) {
let script = r#"
2 > 1 &&
"something" != "nothing" ||
"2014-01-20" < "Wed Jul 8 23:07:35 MDT 2015" &&
2020-07-29 10:09:48 +02:00
[array, has, spaces].len <= #{prop:name}.len &&
2020-05-20 05:12:35 +02:00
modifierTest + 1000 / 2 > (80 * 100 % 2)
"#;
let mut engine = Engine::new();
engine.set_optimization_level(OptimizationLevel::Full);
let ast = engine.compile_expression(script).unwrap();
bench.iter(|| engine.run_ast(&ast).unwrap());
2020-05-20 05:12:35 +02:00
}
#[bench]
fn bench_eval_call_expression(bench: &mut Bencher) {
let script = r#"
2 > 1 &&
"something" != "nothing" ||
"2014-01-20" < "Wed Jul 8 23:07:35 MDT 2015" &&
2020-07-29 10:09:48 +02:00
[array, has, spaces].len <= #{prop:name}.len &&
2020-05-20 05:12:35 +02:00
modifierTest + 1000 / 2 > (80 * 100 % 2)
"#;
2020-05-23 12:59:28 +02:00
let engine = Engine::new();
2020-05-20 05:12:35 +02:00
bench.iter(|| engine.eval_expression::<bool>(script).unwrap());
}
#[bench]
fn bench_eval_call(bench: &mut Bencher) {
let script = r#"
2 > 1 &&
"something" != "nothing" ||
"2014-01-20" < "Wed Jul 8 23:07:35 MDT 2015" &&
2020-07-29 10:09:48 +02:00
[array, has, spaces].len <= #{prop:name}.len &&
2020-05-20 05:12:35 +02:00
modifierTest + 1000 / 2 > (80 * 100 % 2)
"#;
2020-05-23 12:59:28 +02:00
let engine = Engine::new();
2020-05-20 05:12:35 +02:00
bench.iter(|| engine.eval::<bool>(script).unwrap());
}
2020-05-25 11:01:39 +02:00
#[bench]
fn bench_eval_loop_number(bench: &mut Bencher) {
2021-12-30 15:53:59 +01:00
let script = "
2020-05-25 11:01:39 +02:00
let s = 0;
2021-12-15 05:06:17 +01:00
for x in 0..10000 {
2020-05-25 11:01:39 +02:00
s += 1;
}
2021-12-30 15:53:59 +01:00
";
2020-05-25 11:01:39 +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-05-25 11:01:39 +02:00
}
#[bench]
fn bench_eval_loop_strings_build(bench: &mut Bencher) {
let script = r#"
2021-04-23 17:37:44 +02:00
let s;
2021-12-15 05:06:17 +01:00
for x in 0..10000 {
2021-04-23 17:37:44 +02:00
s = "hello, world!" + "hello, world!";
2020-05-25 11:01:39 +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-05-25 11:01:39 +02:00
}
#[bench]
fn bench_eval_loop_strings_no_build(bench: &mut Bencher) {
let script = r#"
2021-04-23 17:37:44 +02:00
let s;
2021-12-15 05:06:17 +01:00
for x in 0..10000 {
2021-04-23 17:37:44 +02:00
s = "hello" + "";
2020-05-25 11:01:39 +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-05-25 11:01:39 +02:00
}
2020-11-13 12:35:51 +01:00
#[bench]
fn bench_eval_switch(bench: &mut Bencher) {
2021-12-30 15:53:59 +01:00
let script = "
2020-11-13 12:35:51 +01:00
let sum = 0;
2020-11-14 10:22:01 +01:00
let rem = 0;
2020-11-13 12:35:51 +01:00
2021-12-15 05:06:17 +01:00
for x in 0..10 {
2020-12-11 05:51:28 +01:00
rem = x % 10;
2020-11-14 10:22:01 +01:00
sum += switch rem {
2020-11-13 12:35:51 +01:00
0 => 10,
1 => 12,
2 => 42,
3 => 1,
2020-12-11 05:51:28 +01:00
4 => 12,
5 => 42,
6 => 1,
7 => 12,
8 => 42,
9 => 1,
2020-11-13 12:35:51 +01:00
}
}
2021-12-30 15:53:59 +01:00
";
2020-11-13 12:35:51 +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());
2020-11-13 12:35:51 +01:00
}
#[bench]
fn bench_eval_nested_if(bench: &mut Bencher) {
2021-12-30 15:53:59 +01:00
let script = "
2020-11-13 12:35:51 +01:00
let sum = 0;
2020-11-14 10:22:01 +01:00
let rem = 0;
2020-11-13 12:35:51 +01:00
2021-12-15 05:06:17 +01:00
for x in 0..10 {
2020-12-11 05:51:28 +01:00
rem = x % 10;
sum += if rem == 0 { 10 }
else if rem == 1 { 12 }
else if rem == 2 { 42 }
else if rem == 3 { 1 }
else if rem == 4 { 12 }
else if rem == 5 { 42 }
else if rem == 6 { 1 }
else if rem == 7 { 12 }
else if rem == 8 { 42 }
else if rem == 9 { 1 };
2020-11-13 12:35:51 +01:00
}
2021-12-30 15:53:59 +01:00
";
2020-11-13 12:35:51 +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());
2020-11-13 12:35:51 +01:00
}