File: base.rs

package info (click to toggle)
rust-base-x 0.2.11-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 140 kB
  • sloc: makefile: 2
file content (75 lines) | stat: -rw-r--r-- 1,597 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#[macro_use]
extern crate bencher;
extern crate base_x;
extern crate rand;

use base_x::{decode, encode, Alphabet};
use bencher::Bencher;

fn random_input(size: usize) -> Vec<u8> {
    let mut v = vec![0; size];

    for x in v.iter_mut() {
        *x = rand::random()
    }

    v
}

fn test_decode<A: Alphabet + Copy>(bench: &mut Bencher, alph: A) {
    let input = random_input(100);
    let out = encode(alph, &input);

    bench.iter(|| decode(alph, &out).unwrap());
}

fn test_encode<A: Alphabet + Copy>(bench: &mut Bencher, alph: A) {
    let input = random_input(100);

    bench.iter(|| encode(alph, &input));
}

// Actual benchmarks

// Encode UTF-8
fn encode_base2(bench: &mut Bencher) {
    const ALPH: &str = "01";
    test_encode(bench, ALPH);
}

fn encode_base16(bench: &mut Bencher) {
    const ALPH: &str = "0123456789abcdef";
    test_encode(bench, ALPH);
}

fn encode_base58(bench: &mut Bencher) {
    const ALPH: &str = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz";
    test_encode(bench, ALPH);
}

// Decode UTF-8
fn decode_base2(bench: &mut Bencher) {
    const ALPH: &str = "01";
    test_decode(bench, ALPH);
}

fn decode_base16(bench: &mut Bencher) {
    const ALPH: &str = "0123456789abcdef";
    test_decode(bench, ALPH);
}

fn decode_base58(bench: &mut Bencher) {
    const ALPH: &str = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz";
    test_decode(bench, ALPH);
}

benchmark_group!(
    benches,
    encode_base2,
    decode_base2,
    encode_base16,
    decode_base16,
    encode_base58,
    decode_base58
);
benchmark_main!(benches);