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.
|
|
|
|
|
2021-12-30 15:53:59 +01:00
|
|
|
const SCRIPT: &str = "
|
|
|
|
const MAX_NUMBER_TO_CHECK = 1_000; // 168 primes <= 1000
|
2020-04-18 09:30:43 +02:00
|
|
|
|
2021-12-30 15:53:59 +01:00
|
|
|
let prime_mask = [];
|
|
|
|
prime_mask.pad(MAX_NUMBER_TO_CHECK, true);
|
2020-04-18 09:30:43 +02:00
|
|
|
|
2021-12-30 15:53:59 +01:00
|
|
|
prime_mask[0] = false;
|
|
|
|
prime_mask[1] = false;
|
2020-04-18 09:30:43 +02:00
|
|
|
|
2021-12-30 15:53:59 +01:00
|
|
|
let total_primes_found = 0;
|
2020-04-18 09:30:43 +02:00
|
|
|
|
2021-12-30 15:53:59 +01:00
|
|
|
for p in 2..MAX_NUMBER_TO_CHECK {
|
|
|
|
if prime_mask[p] {
|
|
|
|
total_primes_found += 1;
|
|
|
|
let i = 2 * p;
|
2020-04-18 09:30:43 +02:00
|
|
|
|
2021-12-30 15:53:59 +01:00
|
|
|
while i < MAX_NUMBER_TO_CHECK {
|
|
|
|
prime_mask[i] = false;
|
|
|
|
i += p;
|
|
|
|
}
|
2020-04-18 09:30:43 +02:00
|
|
|
}
|
|
|
|
}
|
2021-12-30 15:53:59 +01:00
|
|
|
";
|
2020-04-18 09:30:43 +02:00
|
|
|
|
|
|
|
#[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();
|
|
|
|
|
2021-08-06 08:46:27 +02:00
|
|
|
bench.iter(|| engine.run_ast(&ast).unwrap());
|
2020-04-18 09:30:43 +02:00
|
|
|
}
|