Skip to content

Commit

Permalink
benches: Add another comparison
Browse files Browse the repository at this point in the history
  • Loading branch information
udoprog committed Nov 3, 2024
1 parent 6b31eda commit 209cc47
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 17 deletions.
6 changes: 4 additions & 2 deletions benches/benches/comparison.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
}};
Expand All @@ -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,
}
51 changes: 51 additions & 0 deletions benches/benches/comparisons/eval.rs
Original file line number Diff line number Diff line change
@@ -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::<String>();
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::<String>(value)?;
assert_eq!(value, "jude");
Ok::<_, rune::runtime::RuntimeError>(value)
})
});
}
32 changes: 17 additions & 15 deletions benches/benches/comparisons/primes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(|| {
Expand Down

0 comments on commit 209cc47

Please sign in to comment.