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 8db068c
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 13 deletions.
20 changes: 10 additions & 10 deletions grammars/Cargo.toml
@@ -1,29 +1,29 @@
[package]
name = "pest_grammars"
authors = ["Dragoș Tiselice <dragostiselice@gmail.com>"]
categories = ["parsing"]
description = "pest popular grammar implementations"
version = "2.5.2"
documentation = "https://docs.rs/pest"
edition = "2021"
authors = ["Dragoș Tiselice <dragostiselice@gmail.com>"]
homepage = "https://pest.rs/"
repository = "https://github.com/pest-parser/pest"
documentation = "https://docs.rs/pest"
keywords = ["pest", "parser", "peg", "grammar"]
categories = ["parsing"]
license = "MIT/Apache-2.0"
name = "pest_grammars"
readme = "_README.md"
repository = "https://github.com/pest-parser/pest"
rust-version = "1.56"
version = "2.5.2"

[dependencies]
pest = { path = "../pest", version = "2.5.2" }
pest_derive = { path = "../derive", version = "2.5.2" }
pest = {path = "../pest", version = "2.5.2", features = ["fast-line-col"]}
pest_derive = {path = "../derive", version = "2.5.2"}

[dev-dependencies]
criterion = "0.3"

[[bench]]
name = "json"
harness = false
name = "json"

[[bench]]
harness = false
name = "http"
harness = false
18 changes: 15 additions & 3 deletions grammars/benches/json.rs
Expand Up @@ -62,10 +62,22 @@ fn line_col_benchmark(c: &mut Criterion) {

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 8db068c

Please sign in to comment.