Boost logo

Boost Users :

From: Jeff Garland (jeff_at_[hidden])
Date: 2007-11-09 14:33:08


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


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net