Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 37 additions & 34 deletions benches/operations.rs
Original file line number Diff line number Diff line change
@@ -1,90 +1,93 @@
use criterion::{black_box, criterion_group, criterion_main, Criterion};
use criterion::{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| {
.bench_function("Chunk", |b| {
b.iter(|| {
let mut chunk = Chunk::default();
for i in 0..10000 {
for i in 0..N {
chunk = chunk.append(i.to_string());
}

black_box(chunk);
chunk.as_vec()
})
})
.bench_function("vec_append", |b| {
.bench_function("Vec", |b| {
b.iter(|| {
let mut vec = Vec::new();
for i in 0..10000 {
for i in 0..N {
vec.push(i.to_string());
}
black_box(vec);

vec
})
});

// Benchmark prepend operations
c.benchmark_group("prepend")
.bench_function("chunk_prepend", |b| {
.bench_function("Chunk", |b| {
b.iter(|| {
let mut chunk = Chunk::default();
for i in 0..10000 {
for i in 0..N {
chunk = chunk.prepend(i.to_string());
}
black_box(chunk);
chunk.as_vec()
})
})
.bench_function("vec_prepend", |b| {
.bench_function("Vec", |b| {
b.iter(|| {
let mut vec = Vec::new();
for i in 0..10000 {
for i in 0..N {
vec.insert(0, i.to_string());
}
black_box(vec);
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();
.bench_function("Chunk", |b| {
let part: Chunk<_> = (0..100).map(|i| i.to_string()).collect();
b.iter(|| {
black_box(chunk1.clone().concat(chunk2.clone()));
let mut chunk = Chunk::default();
for _ in 0..N {
chunk = chunk.concat(part.clone());
}

chunk.as_vec()
})
})
.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();
.bench_function("Vec", |b| {
let part: Vec<_> = (0..100).map(|i| i.to_string()).collect();
b.iter(|| {
let mut result = vec1.clone();
result.extend(vec2.iter().cloned());
black_box(result)
let mut result = Vec::new();
for _ in 0..N {
result.extend(part.iter().cloned());
}

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("Chunk", |b| {
let chunk: Chunk<_> = (0..N).collect();
b.iter(|| chunk.clone().as_vec())
})
.bench_function("vec_clone", |b| {
let vec: Vec<_> = (0..10000).collect();
b.iter(|| {
black_box(vec.clone());
})
.bench_function("Vec", |b| {
let vec: Vec<_> = (0..N).collect();
b.iter(|| vec.clone())
});

// Benchmark from_iter operation
c.benchmark_group("from_iter")
.bench_function("Chunk", |b| {
b.iter(|| Chunk::from_iter((0..N).into_iter()));
b.iter(|| Chunk::from_iter((0..N).into_iter()).as_vec());
})
.bench_function("Vec", |b| b.iter(|| Vec::from_iter((0..N).into_iter())));
}
Expand Down