File: benches.rs

package info (click to toggle)
rustc 1.85.0%2Bdfsg3-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental, forky, sid, trixie
  • size: 893,396 kB
  • sloc: xml: 158,127; python: 35,830; javascript: 19,497; cpp: 19,002; sh: 17,245; ansic: 13,127; asm: 4,376; makefile: 1,051; perl: 29; lisp: 29; ruby: 19; sql: 11
file content (83 lines) | stat: -rw-r--r-- 2,677 bytes parent folder | download | duplicates (10)
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
76
77
78
79
80
81
82
83
extern crate criterion;
extern crate diff;

use criterion::Criterion;

criterion::criterion_group!(benches, bench_slice, bench_chars, bench_real_world);
criterion::criterion_main!(benches);

fn bench_slice(c: &mut Criterion) {
    c.bench_function("empty", |b| {
        let slice = [0u8; 0];
        b.iter(|| ::diff::slice(&slice, &slice));
    });

    c.bench_function("10 equal items", |b| {
        let slice = [0u8; 10];
        b.iter(|| ::diff::slice(&slice, &slice));
    });

    c.bench_function("10 non-equal items", |b| {
        let (left, right) = ([0u8; 10], [1u8; 10]);
        b.iter(|| ::diff::slice(&left, &right));
    });

    c.bench_function("100 equal items", |b| {
        let slice = [0u8; 100];
        b.iter(|| ::diff::slice(&slice, &slice));
    });

    c.bench_function("100 non-equal items", |b| {
        let (left, right) = ([0u8; 100], [1u8; 100]);
        b.iter(|| ::diff::slice(&left, &right));
    });

    c.bench_function("1000 equal items", |b| {
        let slice = [0u8; 1000];
        b.iter(|| ::diff::slice(&slice, &slice));
    });

    c.bench_function("1000 non-equal items", |b| {
        let (left, right) = ([0u8; 1000], [1u8; 1000]);
        b.iter(|| ::diff::slice(&left, &right));
    });
}

fn bench_chars(c: &mut Criterion) {
    c.bench_function("1024 byte string, last 256 different", |b| {
        let left = "?".repeat(768) + &"_".repeat(256);
        let right = "?".repeat(768) + &"!".repeat(256);
        assert_eq!(left.len(), right.len());
        b.iter(|| ::diff::chars(&left, &right));
    });
}

fn bench_real_world(c: &mut Criterion) {
    let gitignores = std::fs::read_to_string("tests/data/gitignores.txt")
        .unwrap()
        .split("!!!")
        .filter_map(|str| (!str.is_empty()).then(|| str.into()))
        .collect::<Vec<String>>();

    c.bench_function("diff::lines on gitignore files from rust-lang/rust", |b| {
        b.iter(|| {
            for (i, left) in gitignores.iter().enumerate() {
                // diff with previous 3, itself, and next 3
                for right in gitignores[i.saturating_sub(3)..(i + 3).min(gitignores.len())].iter() {
                    ::diff::lines(&left, &right);
                }
            }
        })
    });

    c.bench_function("diff::chars on gitignore files from rust-lang/rust", |b| {
        b.iter(|| {
            for (i, left) in gitignores.iter().enumerate() {
                // diff with previous 2, itself, and next 2
                for right in gitignores[i.saturating_sub(2)..(i + 2).min(gitignores.len())].iter() {
                    ::diff::chars(&left, &right);
                }
            }
        })
    });
}