[date_time] absolute value for time_duration?
What is the best way to ensure that one has a positive time_duration? I'm dealing with hundreds of thousands of data points and need to gather the points that are temporally near a given time (based on a duration). For example, find all points in the collection that are within 1 hour of Feb 2, 5:00pm. I used the following code to verify that simply multiplying by -1 would give the desired result, but it would probably be nicer to provide a std::abs overload. boost::posix_time::ptime lhs1(boost::gregorian::date(2007, 2, 1), boost::posix_time::hours(1)); boost::posix_time::ptime rhs1(boost::gregorian::date(2007, 2, 2), boost::posix_time::hours(1)); boost::posix_time::time_duration elapsed1 = lhs1 - rhs1; elapsed1 *= -1; boost::posix_time::ptime lhs2(boost::gregorian::date(2007, 2, 2), boost::posix_time::hours(1)); boost::posix_time::ptime rhs2(boost::gregorian::date(2007, 2, 1), boost::posix_time::hours(1)); boost::posix_time::time_duration elapsed2 = lhs2 - rhs2; assert(elapsed1 == elapsed2); and the actual code is something like (where duration would be 1 hour in this example): boost::posix_time::time_duration elapsed = pt.timestamp - rhs.timestamp; if (elapsed.is_negative()) elapsed *= -1; return elapsed <= duration; I realize that one could also use std::max and std::min, but that seems a bit inefficient, especially since this is a very performance critical area of our application. Thanks, --Michael Fawcett
On Fri, 9 Nov 2007 12:46:55 -0500, Michael Fawcett wrote
What is the best way to ensure that one has a positive time_duration?
I'm dealing with hundreds of thousands of data points and need to gather the points that are temporally near a given time (based on a duration). For example, find all points in the collection that are within 1 hour of Feb 2, 5:00pm. I used the following code to verify that simply multiplying by -1 would give the desired result, but it would probably be nicer to provide a std::abs overload. ...snip detail...
boost::posix_time::time_duration elapsed = pt.timestamp - rhs.timestamp; if (elapsed.is_negative()) elapsed *= -1; return elapsed <= duration;
I realize that one could also use std::max and std::min, but that seems a bit inefficient, especially since this is a very performance critical area of our application.
I think what you've written is about as efficient as it can be. The abs function is simply a subset of what you have inline time_duration abs(time_duration td) { if (elapsed.is_negative()) return td *= -1; } return td; } I guess you are suggesting that abs should be in date_time? Jeff
On Nov 9, 2007 2:33 PM, Jeff Garland
I guess you are suggesting that abs should be in date_time?
It makes sense to me, but of course that's up to you to decide. Thanks for the help, for now I'll just profile to see which method is the fastest and use that one. --Michael Fawcett P.S. - It's a great library!
participants (2)
-
Jeff Garland
-
Michael Fawcett