File: benchmark.rs

package info (click to toggle)
rust-unicode-truncate 2.0.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 176 kB
  • sloc: makefile: 4
file content (45 lines) | stat: -rw-r--r-- 1,445 bytes parent folder | download
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
/*
use std::time::Duration;

use criterion::{black_box, criterion_group, criterion_main, Criterion, Throughput};
use unicode_truncate::UnicodeTruncateStr;

fn roughly_cut(str: &str, size: usize) -> &str {
    if size >= str.len() {
        return str;
    }
    let mut end = size;
    while !str.is_char_boundary(end) {
        end += 1;
    }
    &str[..end]
}

fn criterion_benchmark(criterion: &mut Criterion) {
    const KB: usize = 1024;
    const TEXT: &str = include_str!("data/zhufu.txt");

    for &size in &[KB, 2 * KB, 4 * KB, 8 * KB, 16 * KB, 28 * KB] {
        let mut group = criterion.benchmark_group(format!("zhu fu/{size}"));
        group
            .sample_size(1000)
            .measurement_time(Duration::from_secs(20))
            .throughput(Throughput::Bytes(size as u64));
        let input = roughly_cut(TEXT, size);
        let max_width = input.len() / 2;
        group.bench_function("end", |bench| {
            bench.iter(|| black_box(input).unicode_truncate(black_box(max_width)));
        });
        group.bench_function("start", |bench| {
            bench.iter(|| black_box(input).unicode_truncate_start(black_box(max_width)));
        });
        group.bench_function("centered", |bench| {
            bench.iter(|| black_box(input).unicode_truncate_centered(black_box(max_width)));
        });
        group.finish();
    }
}

criterion_group!(benches, criterion_benchmark);
criterion_main!(benches);
*/