develooper Front page | perl.datetime | Postings from February 2017

Relative speed of DateTime::SpanSet->contains vs.DateTime::Set->next

Thread Next
Eric L. N. Jensen
February 16, 2017 15:06
Relative speed of DateTime::SpanSet->contains vs.DateTime::Set->next
Message ID:
Hi all,

I have some code that needs to determine whether a bunch of events occur during the day or the night.  My current approach is to find the times of all the sunsets and sunrises occurring during the desired time window, and create two DateTime::Set objects, one for sunrises and one for sunsets.  Then for a given event in question ($dt), I call $sunrises->next( $dt ) and $sunsets->next( $dt ) on those sets to find the next sunrise and the next sunset after that event.  The ordering of the next sunrise and sunset tells me whether at the time of that event it’s currently daytime (next sunset comes before next sunrise) or night (next sunrise before next sunset). 

It occurs to me a that different way to handle this would be to use those sunrises and sunsets to create a set of spans, each representing one night.  Then I could call “contains->($dt)” on that SpanSet to see if the event falls during one of the night intervals.  This would be a single call, rather than the two calls to “next” with my current method, but on the other hand there would be a little additional overhead in building the SpanSet.  (But probably not too much given that I’m already calculating all the necessary DateTimes anyway.) 

Does anyone have any idea (a priori from knowledge of module internals, or based on past empirical experience) whether the SpanSet->contains() method is likely to be any faster than the Set->next() method?  The code is working fine with the current method, so before I rewrite it I thought I’d check to see if anyone here had any thoughts about this. 

If it makes a difference, the number of nights under consideration (and thus the size of the sets or spansets I’m comparing against) will typically be relatively small (from a few up to a few tens of nights), but I’ll need to make thousands of comparisons against the sets. 

Thanks in advance for your help with this,

Thread Next Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About