|
1 |
| -use criterion::{criterion_group, criterion_main, Criterion}; |
| 1 | +use criterion::{criterion_group, criterion_main, Criterion, Throughput}; |
2 | 2 | use rustc_hex::{FromHex, ToHex};
|
3 | 3 |
|
4 | 4 | const DATA: &[u8] = include_bytes!("../src/lib.rs");
|
5 | 5 |
|
6 | 6 | fn bench_encode(c: &mut Criterion) {
|
7 |
| - c.bench_function("hex_encode", |b| b.iter(|| hex::encode(DATA))); |
| 7 | + let mut group = c.benchmark_group("encode"); |
| 8 | + group.throughput(Throughput::Bytes(DATA.len() as u64)); |
8 | 9 |
|
9 |
| - c.bench_function("rustc_hex_encode", |b| b.iter(|| DATA.to_hex::<String>())); |
| 10 | + group.bench_function("hex", |b| b.iter(|| hex::encode(DATA))); |
10 | 11 |
|
11 |
| - c.bench_function("faster_hex_encode", |b| { |
12 |
| - b.iter(|| faster_hex::hex_string(DATA).unwrap()) |
13 |
| - }); |
| 12 | + group.bench_function("rustc_hex", |b| b.iter(|| DATA.to_hex::<String>())); |
| 13 | + |
| 14 | + group.bench_function("faster_hex", |b| b.iter(|| faster_hex::hex_string(DATA))); |
14 | 15 |
|
15 |
| - c.bench_function("faster_hex_encode_fallback", |b| { |
| 16 | + group.bench_function("faster_hex/fallback", |b| { |
16 | 17 | b.iter(|| {
|
17 | 18 | let mut dst = vec![0; DATA.len() * 2];
|
18 | 19 | faster_hex::hex_encode_fallback(DATA, &mut dst);
|
19 | 20 | dst
|
20 | 21 | })
|
21 | 22 | });
|
| 23 | + |
| 24 | + group.bench_function("data_encoding", |b| { |
| 25 | + b.iter(|| data_encoding::HEXLOWER.encode(DATA)) |
| 26 | + }); |
| 27 | + |
| 28 | + group.finish() |
22 | 29 | }
|
23 | 30 |
|
24 | 31 | fn bench_decode(c: &mut Criterion) {
|
25 |
| - c.bench_function("hex_decode", |b| { |
| 32 | + let mut group = c.benchmark_group("decode"); |
| 33 | + group.throughput(Throughput::Bytes(DATA.len() as u64)); |
| 34 | + |
| 35 | + group.bench_function("hex", |b| { |
26 | 36 | let hex = hex::encode(DATA);
|
27 | 37 | b.iter(|| hex::decode(&hex).unwrap())
|
28 | 38 | });
|
29 | 39 |
|
30 |
| - c.bench_function("rustc_hex_decode", |b| { |
| 40 | + group.bench_function("rustc_hex", |b| { |
31 | 41 | let hex = DATA.to_hex::<String>();
|
32 | 42 | b.iter(|| hex.from_hex::<Vec<u8>>().unwrap())
|
33 | 43 | });
|
34 | 44 |
|
35 |
| - c.bench_function("faster_hex_decode", move |b| { |
36 |
| - let hex = faster_hex::hex_string(DATA).unwrap(); |
| 45 | + group.bench_function("faster_hex", move |b| { |
| 46 | + let hex = faster_hex::hex_string(DATA); |
37 | 47 | let len = DATA.len();
|
38 | 48 | let mut dst = vec![0; len];
|
39 | 49 |
|
40 | 50 | b.iter(|| faster_hex::hex_decode(hex.as_bytes(), &mut dst).unwrap())
|
41 | 51 | });
|
42 | 52 |
|
43 |
| - c.bench_function("faster_hex_decode_unchecked", |b| { |
44 |
| - let hex = faster_hex::hex_string(DATA).unwrap(); |
| 53 | + group.bench_function("faster_hex/unchecked", |b| { |
| 54 | + let hex = faster_hex::hex_string(DATA); |
45 | 55 | let len = DATA.len();
|
46 | 56 | let mut dst = vec![0; len];
|
47 | 57 |
|
48 | 58 | b.iter(|| faster_hex::hex_decode_unchecked(hex.as_bytes(), &mut dst))
|
49 | 59 | });
|
50 | 60 |
|
51 |
| - c.bench_function("faster_hex_decode_fallback", |b| { |
52 |
| - let hex = faster_hex::hex_string(DATA).unwrap(); |
| 61 | + group.bench_function("faster_hex/fallback", |b| { |
| 62 | + let hex = faster_hex::hex_string(DATA); |
53 | 63 | let len = DATA.len();
|
54 | 64 | let mut dst = vec![0; len];
|
55 | 65 |
|
56 | 66 | b.iter(|| faster_hex::hex_decode_fallback(hex.as_bytes(), &mut dst))
|
57 | 67 | });
|
| 68 | + |
| 69 | + group.bench_function("data_encoding", |b| { |
| 70 | + let hex = data_encoding::HEXLOWER.encode(DATA); |
| 71 | + b.iter(|| data_encoding::HEXLOWER.decode(hex.as_bytes()).unwrap()) |
| 72 | + }); |
| 73 | + |
| 74 | + group.finish() |
58 | 75 | }
|
59 | 76 |
|
60 | 77 | criterion_group!(benches, bench_encode, bench_decode);
|
|
0 commit comments