-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathcomparisons.rs
107 lines (92 loc) · 2.9 KB
/
comparisons.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#[macro_use]
extern crate criterion;
extern crate dynstack;
use criterion::Bencher;
use criterion::Criterion;
use dynstack::{DynStack, dyn_push};
use std::fmt::Display;
fn push_speed_naive(b: &mut Bencher) {
let mut vec = Vec::<Box<Display>>::new();
b.iter(|| {
vec.push(Box::new(0xF00BAAusize));
vec.push(Box::new(0xABBAu16));
vec.push(Box::new(0xBA7123AAu32));
vec.push(Box::new(12u8));
});
}
fn push_speed_dynstack(b: &mut Bencher) {
let mut stack = DynStack::<Display>::new();
b.iter(|| {
dyn_push!(stack, 0xF00BAAusize);
dyn_push!(stack, 0xABBAu16);
dyn_push!(stack, 0xBA7123AAu32);
dyn_push!(stack, 12u8);
});
}
fn push_and_run_naive(b: &mut Bencher) {
b.iter(|| {
let mut stack = Vec::<Box<Fn() -> usize>>::new();
fn pseudorecursive(stack: &mut Vec<Box<Fn() -> usize>>, n: usize) {
stack.push(Box::new(move || n - 1));
}
let mut n = 100;
let mut i = 0;
while n > 0 {
pseudorecursive(&mut stack, n);
n = (stack.get(i).unwrap())();
i += 1;
}
});
}
fn push_and_run_dynstack(b: &mut Bencher) {
b.iter(|| {
let mut stack = DynStack::<Fn() -> usize>::new();
fn pseudorecursive(stack: &mut DynStack<Fn() -> usize>, n: usize) {
dyn_push!(stack, move || n - 1);
}
let mut n = 100;
let mut i = 0;
while n > 0 {
pseudorecursive(&mut stack, n);
n = (stack.get(i).unwrap())();
i += 1;
}
});
}
fn pseudorecursive2_naive(b: &mut Bencher) {
b.iter(|| {
let mut state: Box<Fn() -> usize> = Box::new(|| 0);
fn pseudorecursive(state: &mut Box<Fn() -> usize>, n: usize) {
*state = Box::new(move || n - 1);
}
let mut n = 100;
while n > 0 {
pseudorecursive(&mut state, n);
n = state();
}
});
}
fn pseudorecursive2_dynstack(b: &mut Bencher) {
b.iter(|| {
let mut stack = DynStack::<Fn() -> usize>::new();
fn pseudorecursive(stack: &mut DynStack<Fn() -> usize>, n: usize) {
dyn_push!(stack, move || n - 1);
}
let mut n = 100;
while n > 0 {
pseudorecursive(&mut stack, n);
n = (stack.peek().unwrap())();
stack.remove_last();
}
});
}
fn criterion_benchmark(c: &mut Criterion) {
c.bench_function("push_speed_naive", push_speed_naive);
c.bench_function("push_speed_dynstack", push_speed_dynstack);
c.bench_function("push_and_run_naive", push_and_run_naive);
c.bench_function("push_and_run_dynstack", push_and_run_dynstack);
c.bench_function("pseudorecursive2_naive", pseudorecursive2_naive);
c.bench_function("pseudorecursive2_dynstack", pseudorecursive2_dynstack);
}
criterion_group!(benches, criterion_benchmark);
criterion_main!(benches);