pydantic / speedate

Fast and simple datetime, date, time and duration parsing for rust.
https://docs.rs/speedate/latest/speedate/
MIT License
199 stars 17 forks source link

Optimise display methods #17

Closed zhuxiujia closed 2 years ago

zhuxiujia commented 2 years ago

impl for #16 I think the time is also important to the performance of the Format string, such as crates the httpdate, It directly affects HTTP Web framework performance

[bench]

fn bench_format_time(bench: &mut Bencher) { let time = Time{ hour: 10, minute: 11, second: 12, microsecond: 11 }; bench.iter(|| { time.to_string(); }) }

[bench]

fn bench_format_date_time(bench: &mut Bencher) { let date = DateTime{ date: Date { year: 2022, month: 7, day: 10 }, time: Time { hour: 0, minute: 0, second: 0, microsecond: 0 }, offset: Some(60) }; bench.iter(|| { date.to_string(); }) }


* befor  change

test bench_format_date ... bench: 149 ns/iter (+/- 3) test bench_format_time ... bench: 231 ns/iter (+/- 4) test bench_format_date_time ... bench: 283 ns/iter (+/- 3) //offset None test bench_format_date_time ... bench: 326 ns/iter (+/- 6) //offset Some(60)

* after change

test bench_format_date ... bench: 51 ns/iter (+/- 0) test bench_format_time ... bench: 53 ns/iter (+/- 3) test bench_format_date_time ... bench: 50 ns/iter (+/- 0) //offset None test bench_format_date_time ... bench: 115 ns/iter (+/- 0) //offset Some(60)


The DateTime format will affect about 50ns due to offset, because it is divided twice of  Memory allocation

f.write_str(std::str::from_utf8(&buf[..]).unwrap())?;


so.If I can fix the DateTime  format string length to Fixed length, then this performance can be improved to 50ns/iter
codecov[bot] commented 2 years ago

Codecov Report

Merging #17 (273f795) into main (d697efc) will not change coverage. The diff coverage is 100.00%.

@@            Coverage Diff            @@
##              main       #17   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files            5         5           
  Lines          617       666   +49     
=========================================
+ Hits           617       666   +49     
Impacted Files Coverage Δ
src/date.rs 100.00% <100.00%> (ø)
src/datetime.rs 100.00% <100.00%> (ø)
src/lib.rs 100.00% <100.00%> (ø)
src/time.rs 100.00% <100.00%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more Δ = absolute <relative> (impact), ø = not affected, ? = missing data Powered by Codecov. Last update d697efc...273f795. Read the comment docs.

samuelcolvin commented 2 years ago

thanks so much 🙏 🚀 .