-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ISO 8601 parsing/formatting for RelativeDuration (#14)
* implement iso 8601 parser for RelativeDuration * implement iso 8601 formatter for RelativeDuration * document iso 8601 parser and formatter * support nanoseconds in duration parser * use correct integer types in RelativeDuration parser * check for integer overflow in RelativeDuration parser * use u32 to represent nanos, to follow chrono::Duration::new signature * support nanoseconds in RelativeDuration formatter * simplify format_spec and improve efficiency * make divmod clearer in RelativeDuration::to_iso_8601 * more test cases for RelativeDuration iso parse/format * add benchmarks for RelativeDuration format/parse * fix quirks of nanosecond handling in RelativeDuration parsing/formatting * add property tests for RelativeDuration parser and formatter
- Loading branch information
Showing
4 changed files
with
422 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
use criterion::{black_box, criterion_group, criterion_main, Criterion}; | ||
|
||
use chronoutil::RelativeDuration; | ||
|
||
fn relative_duration_format_benchmark(c: &mut Criterion) { | ||
let durations = [ | ||
"P1M", | ||
"P1Y1M1W1DT1H1M1S", | ||
"P99999999Y11M30DT23H59M59.999999999S", | ||
] | ||
.iter() | ||
.map(|s| RelativeDuration::from_iso_8601(s).unwrap()) | ||
.collect::<Vec<RelativeDuration>>(); | ||
|
||
let mut g = c.benchmark_group("relative_duration_format"); | ||
|
||
g.bench_function("one_specifier", |b| { | ||
b.iter(|| black_box(durations[0]).to_iso_8601()) | ||
}); | ||
g.bench_function("all_specifiers", |b| { | ||
b.iter(|| black_box(durations[1]).to_iso_8601()) | ||
}); | ||
g.bench_function("long_specifiers", |b| { | ||
b.iter(|| black_box(durations[2]).to_iso_8601()) | ||
}); | ||
} | ||
|
||
fn relative_duration_parse_benchmark(c: &mut Criterion) { | ||
let durations = [ | ||
"P1M", | ||
"P1Y1M1W1DT1H1M1S", | ||
"P99999999Y11M30DT23H59M59.999999999S", | ||
]; | ||
|
||
let mut g = c.benchmark_group("relative_duration_parse"); | ||
|
||
g.bench_function("one_specifier", |b| { | ||
b.iter(|| RelativeDuration::from_iso_8601(black_box(durations[0]))) | ||
}); | ||
g.bench_function("all_specifiers", |b| { | ||
b.iter(|| RelativeDuration::from_iso_8601(black_box(durations[1]))) | ||
}); | ||
g.bench_function("long_specifiers", |b| { | ||
b.iter(|| RelativeDuration::from_iso_8601(black_box(durations[2]))) | ||
}); | ||
} | ||
|
||
criterion_group!( | ||
benches, | ||
relative_duration_format_benchmark, | ||
relative_duration_parse_benchmark | ||
); | ||
criterion_main!(benches); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.