diff --git a/benches/benches/comparison.rs b/benches/benches/comparison.rs index 1a2e2db9b..d96ee0059 100644 --- a/benches/benches/comparison.rs +++ b/benches/benches/comparison.rs @@ -43,9 +43,9 @@ macro_rules! rune_vm { } macro_rules! rhai_ast { - ($($tt:tt)*) => {{ + ($level:ident { $($tt:tt)* }) => {{ let mut engine = $crate::rhai::Engine::new(); - engine.set_optimization_level($crate::rhai::OptimizationLevel::Full); + engine.set_optimization_level($crate::rhai::OptimizationLevel::$level); let ast = engine.compile(stringify!($($tt)*)).unwrap(); $crate::RhaiRunner { engine, ast } }}; @@ -67,9 +67,11 @@ pub(crate) mod rhai { } mod comparisons { + pub mod eval; pub mod primes; } criterion::criterion_main! { comparisons::primes::benches, + comparisons::eval::benches, } diff --git a/benches/benches/comparisons/eval.rs b/benches/benches/comparisons/eval.rs new file mode 100644 index 000000000..4cdf192de --- /dev/null +++ b/benches/benches/comparisons/eval.rs @@ -0,0 +1,51 @@ +use criterion::Criterion; +use rune::Hash; + +criterion::criterion_group!(benches, entry); + +fn entry(b: &mut Criterion) { + let mut group = b.benchmark_group("eval"); + + group.bench_function("rhai", |b| { + let ast = rhai_ast! { + None { + let x = #{ + a: 1, + b: 2.345, + c:"hello", + d: true, + e: #{ x: 42, "y$@#%": (), z: [ 1, 2, 3, #{}, #{ "hey": "jude" }]} + }; + + x["e"].z[4].hey + } + }; + + b.iter(|| { + let out = ast.eval::(); + assert_eq!(out, "jude"); + out + }); + }); + + group.bench_function("rune", |b| { + let mut vm = rune_vm! { + let x = #{ + a: 1, + b: 2.345, + c: "hello", + d: true, + e: #{ x: 42, "y$@#%": (), z: [ 1, 2, 3, #{}, #{ "hey": "jude" }]} + }; + + x["e"].z[4].hey + }; + + b.iter(|| { + let value = vm.call(Hash::EMPTY, ())?; + let value = rune::from_value::(value)?; + assert_eq!(value, "jude"); + Ok::<_, rune::runtime::RuntimeError>(value) + }) + }); +} diff --git a/benches/benches/comparisons/primes.rs b/benches/benches/comparisons/primes.rs index 8ba4454c9..23de649bb 100644 --- a/benches/benches/comparisons/primes.rs +++ b/benches/benches/comparisons/primes.rs @@ -8,29 +8,31 @@ fn entry(b: &mut Criterion) { group.bench_function("rhai", |b| { let ast = rhai_ast! { - const MAX_NUMBER_TO_CHECK = 10_000; + Full { + const MAX_NUMBER_TO_CHECK = 10_000; - let prime_mask = []; - prime_mask.pad(MAX_NUMBER_TO_CHECK, true); + let prime_mask = []; + prime_mask.pad(MAX_NUMBER_TO_CHECK, true); - prime_mask[0] = false; - prime_mask[1] = false; + prime_mask[0] = false; + prime_mask[1] = false; - let total_primes_found = 0; + let total_primes_found = 0; - for p in 2..MAX_NUMBER_TO_CHECK { - if prime_mask[p] { - total_primes_found += 1; - let i = 2 * p; + for p in 2..MAX_NUMBER_TO_CHECK { + if prime_mask[p] { + total_primes_found += 1; + let i = 2 * p; - while i < MAX_NUMBER_TO_CHECK { - prime_mask[i] = false; - i += p; + while i < MAX_NUMBER_TO_CHECK { + prime_mask[i] = false; + i += p; + } } } - } - total_primes_found + total_primes_found + } }; b.iter(|| {