-
Notifications
You must be signed in to change notification settings - Fork 2
/
operations.rs
93 lines (85 loc) · 2.89 KB
/
operations.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
use criterion::{black_box, criterion_group, criterion_main, Criterion};
use tailcall_chunk::Chunk;
const N: usize = 10000;
fn bench_operations(c: &mut Criterion) {
// Benchmark append operations
c.benchmark_group("append")
.bench_function("chunk_append", |b| {
b.iter(|| {
let mut chunk = Chunk::default();
for i in 0..10000 {
chunk = chunk.append(i.to_string());
}
black_box(chunk);
})
})
.bench_function("vec_append", |b| {
b.iter(|| {
let mut vec = Vec::new();
for i in 0..10000 {
vec.push(i.to_string());
}
black_box(vec);
})
});
// Benchmark prepend operations
c.benchmark_group("prepend")
.bench_function("chunk_prepend", |b| {
b.iter(|| {
let mut chunk = Chunk::default();
for i in 0..10000 {
chunk = chunk.prepend(i.to_string());
}
black_box(chunk);
})
})
.bench_function("vec_prepend", |b| {
b.iter(|| {
let mut vec = Vec::new();
for i in 0..10000 {
vec.insert(0, i.to_string());
}
black_box(vec);
})
});
// Benchmark concat operations
c.benchmark_group("concat")
.bench_function("chunk_concat", |b| {
let chunk1: Chunk<_> = (0..5000).map(|i| i.to_string()).collect();
let chunk2: Chunk<_> = (5000..10000).map(|i| i.to_string()).collect();
b.iter(|| {
black_box(chunk1.clone().concat(chunk2.clone()));
})
})
.bench_function("vec_concat", |b| {
let vec1: Vec<_> = (0..5000).map(|i| i.to_string()).collect();
let vec2: Vec<_> = (5000..10000).map(|i| i.to_string()).collect();
b.iter(|| {
let mut result = vec1.clone();
result.extend(vec2.iter().cloned());
black_box(result)
})
});
// Benchmark clone operations
c.benchmark_group("clone")
.bench_function("chunk_clone", |b| {
let chunk: Chunk<_> = (0..10000).collect();
b.iter(|| {
black_box(chunk.clone());
})
})
.bench_function("vec_clone", |b| {
let vec: Vec<_> = (0..10000).collect();
b.iter(|| {
black_box(vec.clone());
})
});
// Benchmark from_iter operation
c.benchmark_group("from_iter")
.bench_function("Chunk", |b| {
b.iter(|| Chunk::from_iter(0..N));
})
.bench_function("Vec", |b| b.iter(|| Vec::from_iter(0..N)));
}
criterion_group!(benches, bench_operations);
criterion_main!(benches);