/
linear.rs
52 lines (44 loc) · 1.89 KB
/
linear.rs
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
use criterion::BenchmarkId;
use criterion::Criterion;
use criterion::{criterion_group, criterion_main};
// The benchmarks here verify that the complexity grows as O(*n*)
// where *n* is the number of characters in the text to be wrapped.
use lipsum::lipsum_words_from_seed;
const LINE_LENGTH: usize = 60;
/// Generate a lorem ipsum text with the given number of characters.
fn lorem_ipsum(length: usize) -> String {
// The average word length in the lorem ipsum text is somewhere
// between 6 and 7. So we conservatively divide by 5 to have a
// long enough text that we can truncate below.
let mut text = lipsum_words_from_seed(length / 5, 42);
text.truncate(length);
text
}
pub fn benchmark(c: &mut Criterion) {
let mut group = c.benchmark_group("String lengths");
for length in [100, 200, 400, 800, 1600].iter() {
let text = lorem_ipsum(*length);
let mut options = textwrap::Options::new(LINE_LENGTH);
group.bench_with_input(BenchmarkId::new("fill", length), &text, |b, text| {
b.iter(|| textwrap::fill(text, &options));
});
group.bench_with_input(BenchmarkId::new("fill_usize", length), &text, |b, text| {
b.iter(|| textwrap::fill(text, *length));
});
#[cfg(feature = "hyphenation")]
{
use hyphenation::{Language, Load, Standard};
let path = std::path::Path::new(env!("CARGO_MANIFEST_DIR"))
.join("benches")
.join("la.standard.bincode");
let dictionary = Standard::from_path(Language::Latin, &path).unwrap();
options.splitter = Box::new(dictionary);
group.bench_with_input(BenchmarkId::new("hyphenation", length), &text, |b, text| {
b.iter(|| textwrap::fill(text, &options));
});
}
}
group.finish();
}
criterion_group!(benches, benchmark);
criterion_main!(benches);