forked from gluon-lang/gluon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunction_call.rs
72 lines (66 loc) · 1.69 KB
/
function_call.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#![feature(test)]
extern crate test;
extern crate gluon;
use gluon::{Compiler, new_vm};
use gluon::vm::api::FunctionRef;
// Benchmarks function calls
#[bench]
fn factorial(b: &mut ::test::Bencher) {
let vm = new_vm();
let text = r#"
let factorial n =
if n < 2
then 1
else n * factorial (n - 1)
factorial
"#;
Compiler::new()
.load_script(&vm, "factorial", text)
.unwrap();
let mut factorial: FunctionRef<fn (i32) -> i32> = vm.get_global("factorial").unwrap();
b.iter(|| {
let result = factorial.call(100).unwrap();
::test::black_box(result)
})
}
#[bench]
fn factorial_tail_call(b: &mut ::test::Bencher) {
let vm = new_vm();
let text = r#"
let factorial a n =
if n < 2
then a
else factorial (a * n) (n - 1)
factorial 1
"#;
Compiler::new()
.load_script(&vm, "factorial", text)
.unwrap();
let mut factorial: FunctionRef<fn (i32) -> i32> = vm.get_global("factorial").unwrap();
b.iter(|| {
let result = factorial.call(100).unwrap();
::test::black_box(result)
})
}
#[bench]
fn gluon_rust_boundary_overhead(b: &mut ::test::Bencher) {
let vm = new_vm();
let text = r#"
let { trim } = import "std/string.glu"
let for n f =
if n #Int== 0 then
()
else
f n
for (n #Int- 10) f
\n -> for n \_ -> trim ""
"#;
Compiler::new()
.load_script(&vm, "test", text)
.unwrap();
let mut factorial: FunctionRef<fn (i32) -> ()> = vm.get_global("test").unwrap();
b.iter(|| {
let result = factorial.call(1000).unwrap();
::test::black_box(result)
})
}