Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use criterion for benchmarking (#21)
* Use criterion for benchmarks * Add faster_hex for benchmarks Signed-off-by: koushiro <koushiro.cqx@gmail.com>
- Loading branch information
Showing
4 changed files
with
122 additions
and
85 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
use criterion::{criterion_group, criterion_main, Criterion}; | ||
use rustc_hex::{FromHex, ToHex}; | ||
|
||
const DATA: &[u8] = include_bytes!("../src/lib.rs"); | ||
|
||
fn bench_encode(c: &mut Criterion) { | ||
c.bench_function("hex_encode", |b| b.iter(|| hex::encode(DATA))); | ||
|
||
c.bench_function("rustc_hex_encode", |b| b.iter(|| DATA.to_hex::<String>())); | ||
|
||
c.bench_function("faster_hex_encode", |b| { | ||
b.iter(|| faster_hex::hex_string(DATA).unwrap()) | ||
}); | ||
|
||
c.bench_function("faster_hex_encode_fallback", |b| { | ||
b.iter(|| { | ||
let mut dst = vec![0; DATA.len() * 2]; | ||
faster_hex::hex_encode_fallback(DATA, &mut dst); | ||
dst | ||
}) | ||
}); | ||
} | ||
|
||
fn bench_decode(c: &mut Criterion) { | ||
c.bench_function("hex_decode", |b| { | ||
let hex = hex::encode(DATA); | ||
b.iter(|| hex::decode(&hex).unwrap()) | ||
}); | ||
|
||
c.bench_function("rustc_hex_decode", |b| { | ||
let hex = DATA.to_hex::<String>(); | ||
b.iter(|| hex.from_hex::<Vec<u8>>().unwrap()) | ||
}); | ||
|
||
c.bench_function("faster_hex_decode", move |b| { | ||
let hex = faster_hex::hex_string(DATA).unwrap(); | ||
let len = DATA.len(); | ||
b.iter(|| { | ||
let mut dst = Vec::with_capacity(len); | ||
dst.resize(len, 0); | ||
faster_hex::hex_decode(hex.as_bytes(), &mut dst).unwrap(); | ||
dst | ||
}) | ||
}); | ||
|
||
c.bench_function("faster_hex_decode_unchecked", |b| { | ||
let hex = faster_hex::hex_string(DATA).unwrap(); | ||
let len = DATA.len(); | ||
b.iter(|| { | ||
let mut dst = Vec::with_capacity(len); | ||
dst.resize(len, 0); | ||
faster_hex::hex_decode_unchecked(hex.as_bytes(), &mut dst); | ||
dst | ||
}) | ||
}); | ||
|
||
c.bench_function("faster_hex_decode_fallback", |b| { | ||
let hex = faster_hex::hex_string(DATA).unwrap(); | ||
let len = DATA.len(); | ||
b.iter(|| { | ||
let mut dst = Vec::with_capacity(len); | ||
dst.resize(len, 0); | ||
faster_hex::hex_decode_fallback(hex.as_bytes(), &mut dst); | ||
dst | ||
}) | ||
}); | ||
} | ||
|
||
criterion_group!(benches, bench_encode, bench_decode); | ||
criterion_main!(benches); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters