rhai/benches/primes.rs

44 lines
917 B
Rust
Raw Normal View History

2020-04-18 09:30:43 +02:00
#![feature(test)]
///! Test evaluating expressions
extern crate test;
use rhai::{Engine, OptimizationLevel};
use test::Bencher;
// This script uses the Sieve of Eratosthenes to calculate prime numbers.
const SCRIPT: &str = r#"
const MAX_NUMBER_TO_CHECK = 1_000; // 168 primes <= 1000
let prime_mask = [];
prime_mask.pad(MAX_NUMBER_TO_CHECK, true);
2020-04-22 14:08:42 +02:00
prime_mask[0] = false;
prime_mask[1] = false;
2020-04-18 09:30:43 +02:00
let total_primes_found = 0;
2021-12-15 05:06:17 +01:00
for p in 2..=MAX_NUMBER_TO_CHECK {
2020-04-18 09:30:43 +02:00
if prime_mask[p] {
total_primes_found += 1;
let i = 2 * p;
while i < MAX_NUMBER_TO_CHECK {
prime_mask[i] = false;
i += p;
}
}
}
"#;
#[bench]
fn bench_eval_primes(bench: &mut Bencher) {
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-04-18 09:30:43 +02:00
}