Skip to content

Commit

Permalink
Don't measure encoding time in decoding benchmarks
Browse files Browse the repository at this point in the history
  • Loading branch information
fbernier committed Nov 20, 2024
1 parent fe287a9 commit 1c952a1
Showing 1 changed file with 47 additions and 33 deletions.
80 changes: 47 additions & 33 deletions benches/base62.rs
Original file line number Diff line number Diff line change
@@ -1,59 +1,74 @@
use base62::{
decode, decode_alternative, /*digit_count,*/ encode, encode_alternative,
encode_alternative_buf, encode_alternative_bytes, encode_buf, encode_bytes,
decode, decode_alternative, encode, encode_alternative, encode_alternative_buf,
encode_alternative_bytes, encode_buf, encode_bytes,
};
use criterion::{black_box, criterion_group, criterion_main, Criterion};
use criterion::{black_box, criterion_group, criterion_main, BatchSize, Criterion};
use rand::distributions::Standard;
use rand::{thread_rng, Rng};

pub fn criterion_benchmark(c: &mut Criterion) {
let mut random_u128s = thread_rng().sample_iter::<u128, Standard>(Standard);
let mut group = c.benchmark_group("decode");

c.bench_function("decode_standard", |b| {
// Fixed input benchmark
group.bench_function("standard_fixed", |b| {
b.iter(|| decode(black_box("7n42DGM5Tflk9n8mt7Fhc7")))
});

c.bench_function("decode_standard_random", |b| {
b.iter(|| decode(black_box(encode(random_u128s.next().unwrap()))))
// Random input benchmark using iter_batched for setup
group.bench_function("standard_random", |b| {
b.iter_batched(
|| {
// Setup (runs outside measured time)
let random_num: u128 = thread_rng().sample(Standard);
encode(random_num)
},
decode, // Measured function
BatchSize::SmallInput,
)
});

c.bench_function("decode_alternative", |b| {
group.bench_function("alternative_fixed", |b| {
b.iter(|| decode_alternative(black_box("7N42dgm5tFLK9N8MT7fHC7")))
});

c.bench_function("decode_alternative_random", |b| {
b.iter(|| decode_alternative(black_box(encode_alternative(random_u128s.next().unwrap()))))
group.bench_function("alternative_random", |b| {
b.iter_batched(
|| {
// Setup (runs outside measured time)
let random_num: u128 = thread_rng().sample(Standard);
encode_alternative(random_num)
},
decode_alternative,
BatchSize::SmallInput,
)
});
group.finish();

/*
c.bench_function("digit_count", |b| {
b.iter(|| digit_count(black_box(random_u128s.next().unwrap())))
});
*/
// Original encoding benchmarks
let mut random_u128s = thread_rng().sample_iter::<u128, Standard>(Standard);

c.bench_function("encode_standard_new", |b| {
let mut group = c.benchmark_group("encode");

group.bench_function("standard_new_fixed", |b| {
b.iter(|| encode(black_box(u128::MAX)))
});

c.bench_function("encode_standard_new_random", |b| {
group.bench_function("standard_new_random", |b| {
b.iter(|| encode(black_box(random_u128s.next().unwrap())))
});

c.bench_function("encode_standard_bytes", |b| {
group.bench_function("standard_bytes_fixed", |b| {
let mut buf = [0; 22];
b.iter(|| encode_bytes(black_box(u128::MAX), black_box(&mut buf)))
});

c.bench_function("encode_standard_bytes_random", |b| {
group.bench_function("standard_bytes_random", |b| {
let mut buf = [0; 22];
b.iter(|| encode_bytes(black_box(random_u128s.next().unwrap()), black_box(&mut buf)))
});

c.bench_function("encode_standard_buf", |b| {
group.bench_function("standard_buf_fixed", |b| {
b.iter(|| encode_buf(black_box(u128::MAX), black_box(&mut String::new())))
});

c.bench_function("encode_standard_buf_random", |b| {
group.bench_function("standard_buf_random", |b| {
b.iter(|| {
encode_buf(
black_box(random_u128s.next().unwrap()),
Expand All @@ -62,38 +77,37 @@ pub fn criterion_benchmark(c: &mut Criterion) {
})
});

c.bench_function("encode_alternative_new", |b| {
group.bench_function("alternative_new_fixed", |b| {
b.iter(|| encode_alternative(black_box(u128::MAX)))
});

c.bench_function("encode_alternative_new_random", |b| {
group.bench_function("alternative_new_random", |b| {
b.iter(|| encode_alternative(black_box(random_u128s.next().unwrap())))
});

c.bench_function("encode_alternative_bytes", |b| {
group.bench_function("alternative_bytes_fixed", |b| {
let mut buf = [0; 22];
b.iter(|| encode_alternative_bytes(black_box(u128::MAX), black_box(&mut buf)))
});

c.bench_function("encode_alternative_bytes_random", |b| {
group.bench_function("alternative_bytes_random", |b| {
let mut buf = [0; 22];
b.iter(|| {
encode_alternative_bytes(black_box(random_u128s.next().unwrap()), black_box(&mut buf))
})
});

c.bench_function("encode_alternative_buf", |b| {
group.bench_function("alternative_buf_fixed", |b| {
b.iter(|| encode_alternative_buf(black_box(u128::MAX), black_box(&mut String::new())))
});

c.bench_function("encode_alternative_buf_random", |b| {
group.bench_function("alternative_buf_random", |b| {
b.iter(|| {
encode_alternative_buf(
black_box(random_u128s.next().unwrap()),
black_box(&mut String::new()),
)
})
});

group.finish();
}

criterion_group!(benches, criterion_benchmark);
Expand Down

0 comments on commit 1c952a1

Please sign in to comment.