2020-10-02 08:55:02 +02:00
|
|
|
#![feature(test)]
|
|
|
|
|
|
|
|
///! Test evaluating with scope
|
|
|
|
extern crate test;
|
|
|
|
|
2022-01-22 16:02:32 +01:00
|
|
|
use rhai::{Engine, Module, OptimizationLevel, Scope};
|
2020-10-02 08:55:02 +02:00
|
|
|
use test::Bencher;
|
|
|
|
|
|
|
|
#[bench]
|
2020-10-03 05:42:54 +02:00
|
|
|
fn bench_eval_module(bench: &mut Bencher) {
|
2021-12-30 15:53:59 +01:00
|
|
|
let script = "
|
2020-10-02 08:55:02 +02:00
|
|
|
fn foo(x) { x + 1 }
|
|
|
|
fn bar(x) { foo(x) }
|
2021-12-30 15:53:59 +01:00
|
|
|
";
|
2020-10-02 08:55:02 +02:00
|
|
|
|
|
|
|
let mut engine = Engine::new();
|
|
|
|
engine.set_optimization_level(OptimizationLevel::None);
|
|
|
|
|
|
|
|
let ast = engine.compile(script).unwrap();
|
|
|
|
|
2022-01-22 16:02:32 +01:00
|
|
|
let module = Module::eval_ast_as_new(Scope::new(), &ast, &engine).unwrap();
|
2020-10-02 08:55:02 +02:00
|
|
|
|
2020-12-23 03:08:43 +01:00
|
|
|
engine.register_static_module("testing", module.into());
|
2020-10-02 08:55:02 +02:00
|
|
|
|
|
|
|
let ast = engine
|
|
|
|
.compile(
|
2021-12-30 15:53:59 +01:00
|
|
|
"
|
2020-10-02 08:55:02 +02:00
|
|
|
fn foo(x) { x - 1 }
|
2020-11-20 03:35:25 +01:00
|
|
|
testing::bar(41)
|
2021-12-30 15:53:59 +01:00
|
|
|
",
|
2020-10-02 08:55:02 +02:00
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
|
2021-08-06 08:46:27 +02:00
|
|
|
bench.iter(|| engine.run_ast(&ast).unwrap());
|
2020-10-02 08:55:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#[bench]
|
|
|
|
fn bench_eval_function_call(bench: &mut Bencher) {
|
|
|
|
let mut engine = Engine::new();
|
|
|
|
engine.set_optimization_level(OptimizationLevel::None);
|
|
|
|
|
|
|
|
let ast = engine
|
|
|
|
.compile(
|
2021-12-30 15:53:59 +01:00
|
|
|
"
|
2020-10-02 08:55:02 +02:00
|
|
|
fn foo(x) { x - 1 }
|
|
|
|
fn bar(x) { foo(x) }
|
|
|
|
bar(41)
|
2021-12-30 15:53:59 +01:00
|
|
|
",
|
2020-10-02 08:55:02 +02:00
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
|
2021-08-06 08:46:27 +02:00
|
|
|
bench.iter(|| engine.run_ast(&ast).unwrap());
|
2020-10-02 08:55:02 +02:00
|
|
|
}
|