Faster way to get number of days between two Dates? #255
-
I have two (date1 - date2).total(SpanTotal::from(Unit::Day).days_are_24_hours()).unwrap() as i64 with chrono I used (date1 - date2).num_days() A quick profile of a benchmark of my code (has some other logic) with
chrono has
I tried the following (which I'm not sure is correct) date1.duration_since(date2).as_hours() / 24 but it's not faster (seems to use the same Is there a faster way to get the number of days between two |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 14 replies
-
It's a little tricky to answer this question without an MRE (please include MREs whenever possible), but if you have two Chrono uses a different representation that requires With that said, this aspect of Jiff hasn't really had much optimization work applied to it yet. |
Beta Was this translation helpful? Give feedback.
OK thanks, that helps a lot.
What do you actually do with the
Date
values? Would it make sense if Jiff provided acivil::UnixEpochDays
? It would effectively provide the samecivil::Date
API, but use a "number of days since the Unix epoch" representation. That would make these "number of days" calculations quite a bit faster. It wouldn't match your epoch of course, but if you just stay in theUnixEpochDays
representation, that will just be a little arithmetic.But yeah, most of the time being spent here is converting between
YYYY-MM-DD
and Unix epoch days.