Skip to content

Commit

Permalink
Merge pull request #5 from ChaiTRex/master
Browse files Browse the repository at this point in the history
105% faster decoding; 7% faster encoding; MSRV lowered to 1.36
  • Loading branch information
fbernier authored Aug 29, 2022
2 parents 5e1a70a + 25d811c commit aaf4ec2
Show file tree
Hide file tree
Showing 3 changed files with 744 additions and 221 deletions.
10 changes: 8 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
[package]
name = "base62"
version = "2.0.0"
authors = ["Francois Bernier <[email protected]>", "Chai T. Rex <[email protected]>"]
authors = [
"François Bernier <[email protected]>",
"Chai T. Rex <[email protected]>",
"Kevin Darlington <[email protected]>",
"Christopher Tarquini <[email protected]>",
]
edition = "2018"
description = "A Base62 encoding/decoding library"
documentation = "https://docs.rs/base62/"
Expand All @@ -15,8 +20,9 @@ exclude = [
]

[dev-dependencies]
quickcheck = "1"
criterion = "0.3.5"
quickcheck = "1"
rand = "0.8.5"

[[bench]]
name = "base62"
Expand Down
59 changes: 52 additions & 7 deletions benches/base62.rs
Original file line number Diff line number Diff line change
@@ -1,31 +1,76 @@
use base62::{
decode, decode_alternative, encode, encode_alternative, encode_alternative_buf, encode_buf,
decode, decode_alternative, /*digit_count,*/ encode, encode_alternative,
encode_alternative_buf, encode_buf,
};
use criterion::{black_box, criterion_group, criterion_main, 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);

c.bench_function("decode_standard", |b| {
b.iter(|| decode(black_box("7n42DGM5Tflk9n8mt7Fhc7")))
});

c.bench_function("decode_standard_random", |b| {
b.iter(|| decode(black_box(encode(random_u128s.next().unwrap()))))
});

c.bench_function("decode_alternative", |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()))))
});

/*
c.bench_function("digit_count", |b| {
b.iter(|| digit_count(black_box(random_u128s.next().unwrap())))
});
*/

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

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

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

c.bench_function("encode_standard_buf_random", |b| {
b.iter(|| {
encode_buf(
black_box(random_u128s.next().unwrap()),
black_box(&mut String::new()),
)
})
});

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

c.bench_function("encode_alternative_buf", |b| {
b.iter(|| encode_alternative_buf(black_box(u128::MAX), black_box(&mut String::new())))
c.bench_function("encode_alternative_new_random", |b| {
b.iter(|| encode_alternative(black_box(random_u128s.next().unwrap())))
});

c.bench_function("decode_standard", |b| {
b.iter(|| decode(black_box("7n42DGM5Tflk9n8mt7Fhc7")))
c.bench_function("encode_alternative_buf", |b| {
b.iter(|| encode_alternative_buf(black_box(u128::MAX), black_box(&mut String::new())))
});

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

Expand Down
Loading

0 comments on commit aaf4ec2

Please sign in to comment.