Skip to content

Commit

Permalink
Add benchmark for pair.line_col vs position.line_cole
Browse files Browse the repository at this point in the history
  • Loading branch information
huacnlee committed Dec 27, 2022
1 parent 6434877 commit e052c89
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 3 deletions.
27 changes: 24 additions & 3 deletions grammars/benches/json.rs
Expand Up @@ -56,16 +56,37 @@ item = _{ SOI ~ line* ~ EOI }
pub struct JsonParser;
}

// With 500 times iter
// pair.line_col time: [2.9937 µs 2.9975 µs 3.0018 µs]
// position.line_col time: [212.59 µs 213.38 µs 214.29 µs]
// position.line_col (with fast-line-col) time: [18.241 µs 18.382 µs 18.655 µs]
//
// With 1000 times iter
// pair.line_col time: [10.814 µs 10.846 µs 10.893 µs]
// position.line_col time: [90.135 µs 93.901 µs 98.655 µs]
// position.line_col (with fast-line-col) time: [1.7199 ms 1.7246 ms 1.7315 ms]
fn line_col_benchmark(c: &mut Criterion) {
let mut file = File::open("benches/main.i18n.json").unwrap();
let mut data = String::new();

file.read_to_string(&mut data).unwrap();
let pairs = autocorrect::JsonParser::parse(autocorrect::Rule::item, &data).unwrap();
let last_pair = pairs.last().unwrap();
c.bench_function("line col", |b| {

c.bench_function("pair.line_col", |b| {
b.iter(|| {
let mut pairs = pairs.clone();
for _ in 0..500 {
pairs.next().unwrap().line_col();
}
})
});

c.bench_function("position.line_col", |b| {
b.iter(|| {
last_pair.line_col();
let mut pairs = pairs.clone();
for _ in 0..500 {
pairs.next().unwrap().as_span().start_pos().line_col();
}
});
});
}
Expand Down
4 changes: 4 additions & 0 deletions pest/src/position.rs
Expand Up @@ -116,6 +116,10 @@ impl<'i> Position<'i> {

/// Returns the line and column number of this `Position`.
///
/// This is an O(n) operation, where n is the number of lines in the input.
/// You better use `pair.line_col()` instead.
///
///
/// # Examples
///
/// ```
Expand Down

0 comments on commit e052c89

Please sign in to comment.