my fork of dmp
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
use core::panic;
use std::path::Path;

use criterion::{criterion_group, criterion_main, Criterion};

fn prefix_linear(a: &str, b: &str, res: usize) {
    let found = a.bytes()
        .zip(b.bytes())
        .take_while(|(a, b)| a == b )
        .count();

    if found != res {
        panic!()
    }
}

fn prefix_binary(a: &str, b: &str, res: usize) {
    let a = a.as_bytes();
    let b = b.as_bytes();

    let mut pointmin = 0;
    let mut pointmax = a.len().min(b.len());
    let mut pointmid = pointmax;

    let mut pointstart = 0;

    while pointmin < pointmid {
        if a[pointstart .. pointmid] ==
            b[pointstart .. pointmid] {
                pointmin = pointmid;
                pointstart = pointmin;
        } else {
            pointmax = pointmid;
        }

        pointmid = (pointmax - pointmin) / 2 + pointmin;
    }

    if pointmid != res {
        panic!("Not desired res")
    }
}

fn create_data() -> Vec<(String, String, usize, usize)> {
    let basedir = Path::new("testdata");
    let data = [100, 1000, 10000, 100000, 1000000, 10000000]
        .iter()
        .map(|&n| {
            let old = std::fs::read_to_string(basedir.join(format!("old_{n}.txt"))).unwrap();
            let new = std::fs::read_to_string(basedir.join(format!("new_{n}.txt"))).unwrap();

            let res = std::fs::read_to_string(basedir.join(format!("ans_{n}.txt"))).unwrap().parse::<usize>().unwrap();

            (old, new, res, n)
        }).collect();

    data
}

pub fn prefix_bench(c: &mut Criterion) {
    let d = create_data();
    
    for (old, new, res, n) in d.iter() {
        println!("For N={n}");
        c.bench_function("prefix linear", |bencher| bencher.iter(|| prefix_linear(old, new, *res)));
        c.bench_function("prefix binary", |bencher| bencher.iter(|| prefix_binary(old, new, *res)));
    }
}


criterion_group!(prefix, prefix_bench);
criterion_main!(prefix);