diff --git a/Cargo.toml b/Cargo.toml index d95514e..d96a0c0 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -25,6 +25,11 @@ winnow = "0.6.8" [dev-dependencies] serde_json = { version = "1.0.87" } +criterion = "0.3" + +[[bench]] +name = "parser_benchmark" +harness = false # docs.rs-specific configuration [package.metadata.docs.rs] diff --git a/README.md b/README.md index 2a37d03..48149f5 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,12 @@ # parse string to std::time::Duration +======================================== +

+ duration-str parser +

[![Chrono GitHub Actions](https://github.com/baoyachi/duration-str-rs/actions/workflows/check.yml/badge.svg)](https://github.com/baoyachi/duration-str-rs/actions?query=workflow%3Abuild) [![Crates.io](https://img.shields.io/crates/v/duration-str.svg)](https://crates.io/crates/duration-str) diff --git a/benches/parser_benchmark.rs b/benches/parser_benchmark.rs new file mode 100644 index 0000000..77cfe03 --- /dev/null +++ b/benches/parser_benchmark.rs @@ -0,0 +1,36 @@ +use criterion::{criterion_group, criterion_main, Criterion}; +use std::time::Duration; +use winnow::ascii::{digit1, multispace0}; +use winnow::error::ContextError; +use winnow::token::literal; +use winnow::Parser; + +fn parse_duration() { + let duration = duration_str::parse("2h 37m").unwrap(); + assert_eq!(duration, Duration::new(9420, 0)) +} + +fn impeccable_duration() { + let input = "2h 37m"; + ( + digit1::<_, ContextError>.try_map(str::parse::), + literal('h').value(3600), + multispace0, + digit1.try_map(str::parse::), + literal('m').value(60), + ) + .map(|(hour, h_unit, _, min, min_unit)| hour * h_unit + min * min_unit) + .parse(input) + .unwrap(); +} + +pub fn duration_str_benchmark(c: &mut Criterion) { + c.bench_function("duration_str", |b| b.iter(|| parse_duration())); +} + +pub fn impeccable_benchmark(c: &mut Criterion) { + c.bench_function("impeccable", |b| b.iter(|| impeccable_duration())); +} + +criterion_group!(benches, duration_str_benchmark, impeccable_benchmark); +criterion_main!(benches); diff --git a/src/lib.rs b/src/lib.rs index 45a8ea8..7d3ec53 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,5 +1,5 @@ #![doc( - html_logo_url = "https://raw.githubusercontent.com/baoyachi/duration-rs/master/duration-str.png" + html_logo_url = "https://raw.githubusercontent.com/baoyachi/duration-str/master/duration-str.png" )] //! Parse string to `Duration` . //!