Add more benchmarks.

This commit is contained in:
Stephen Chung 2020-04-13 23:38:10 +08:00
parent f600e59401
commit 691541c176
4 changed files with 122 additions and 1 deletions

View File

@ -146,6 +146,8 @@ There are also a number of examples scripts that showcase Rhai's features, all i
| -------------------------------------------- | ---------------------------------------------------------------------------------- |
| [`speed_test.rhai`](scripts/speed_test.rhai) | a simple program to measure the speed of Rhai's interpreter (1 million iterations) |
| [`primes.rhai`](scripts/primes.rhai) | use Sieve of Eratosthenes to find all primes smaller than a limit |
| [`fibonacci.rhai`](scripts/fibonacci.rhai) | calculate the n-th Fibonacci number using a really dumb algorithm |
| [`mat_mul.rhai`](scripts/mat_mul.rhai) | matrix multiplication test to measure the speed of Rhai's interpreter |
To run the scripts, either make a tiny program or use of the `rhai_runner` example:

View File

@ -3,7 +3,7 @@
///! Test 1,000 iterations
extern crate test;
use rhai::{Engine, OptimizationLevel};
use rhai::{Engine, OptimizationLevel, Scope, INT};
use test::Bencher;
#[bench]
@ -23,3 +23,27 @@ fn bench_iterations_1000(bench: &mut Bencher) {
bench.iter(|| engine.consume_ast(&ast).unwrap());
}
#[bench]
fn bench_iterations_fibonacci(bench: &mut Bencher) {
let script = r#"
fn fibonacci(n) {
if n < 2 {
n
} else {
fibonacci(n-1) + fibonacci(n-2)
}
}
"#;
let mut engine = Engine::new();
engine.set_optimization_level(OptimizationLevel::None);
let ast = engine.compile(script).unwrap();
bench.iter(|| {
engine
.call_fn::<_, INT>(&mut Scope::new(), &ast, "fibonacci", (20 as INT,))
.unwrap()
});
}

22
scripts/fibonacci.rhai Normal file
View File

@ -0,0 +1,22 @@
// This script calculates the n-th Fibonacci number using a really dumb algorithm
// to test the speed of the scripting engine.
const target = 30;
let now = timestamp();
fn fib(n) {
if n < 2 {
n
} else {
fib(n-1) + fib(n-2)
}
}
print("Ready... Go!");
let result = fib(target);
print("Fibonacci number #" + target + " = " + result);
print("Finished. Run time = " + now.elapsed() + " seconds.");

73
scripts/mat_mul.rhai Normal file
View File

@ -0,0 +1,73 @@
const SIZE = 50;
fn new_mat(x, y) {
let row = [];
row.pad(y, 0.0);
let matrix = [];
matrix.pad(x, row);
matrix
}
fn mat_gen(n) {
let m = new_mat(n, n);
let tmp = 1.0 / n.to_float() / n.to_float();
for i in range(0, n) {
for j in range(0, n) {
let foo = m[i];
foo[j] = tmp * (i.to_float() - j.to_float()) * (i.to_float() + j.to_float());
m[i] = foo;
}
}
m
}
fn mat_mul(a, b) {
let m = a.len();
let n = a[0].len();
let p = b[0].len();
let b2 = new_mat(n, p);
for i in range(0, n) {
for j in range(0, p) {
let foo = b2[j];
foo[i] = b[i][j];
b2[j] = foo;
}
}
let c = new_mat(m, p);
for i in range(0, c.len()) {
let ci = c[i];
for j in range(0, ci.len()) {
let b2j = b2[j];
ci[j] = 0.0;
for z in range(0, a[i].len()) {
let x = a[i][z];
let y = b2j[z];
ci[j] += x * y;
}
}
c[i] = ci;
}
c
}
let now = timestamp();
let a = mat_gen(SIZE);
let b = mat_gen(SIZE);
let c = mat_mul(a, b);
for i in range(0, SIZE) {
print(c[i]);
}
print("Finished. Run time = " + now.elapsed() + " seconds.");