|
Boost : |
From: Carlo Wood (carlo_at_[hidden])
Date: 2006-05-15 10:55:27
On Sun, May 14, 2006 at 05:09:28PM +0200, Silex wrote:
> Would boost be interested by including this class in boost::timer ?
Even more so when it had microsecond precision.
> I'm all ear for your suggestions / bugs / whatever fits it :)
It would be better when the object itself can be handled as variable
(independent of it's internal structure).
For that to work best, there shouldn't be any 'double's in the
internal structure. Two int's, one for seconds and one for microseconds,
would be best, imho. There could be an accessor to get a rounded
double in seconds, ie 'rseconds()' (I'd reserve 'seconds()' for
returning the integer number of seconds.
Now, there is a problem imho with it's type: An absolute time is
different from a relative time. The fact that an absolute time is
relative to 1 Jan 1970 0:00:00 would make one confused- thinking
that the two are the same thing, but I'd argue that it would be
better to make TWO types.
You could use boost::Time for a real time (in UTC), and boost::TimeDiff
to represent time intervals.
For example,
boost::Time start; // Uninitialized object.
assert(!start); // Not initialized.
start.set(); // Set to current time.
assert(start); // Initialized.
// Do work...
boost::TimeDiff elapsed = boost::Time() - start; // Time is automatically initialized when used.
There would be a Time::set(time_t seconds), Time::set(time_t seconds,
suseconds_t microseconds), and Time::set(double seconds);
And of course, we'd have:
Time + Time // error
Time - Time = TimeDiff
Time +/- TimeDiff = Time
TimeDiff + Time = Time
TimeDiff - Time // error
TimeDiff +/- TimeDiff = TimeDiff
Both classes, or just TimeDiff(?), could have accessors for
seconds and microseconds.
It should be possible to compare TimeDiff with an integer (seconds),
assuming 0 for the microseconds part, ie:
TimeDiff elapsed = stop - start;
if (elapsed > 2)
std::cout << "More than 2 seconds have passed.\n";
I'm inclined to think that operator== should NOT be defined
for TimeDiff == int, and certainly not for TimeDiff == double
(I'm afraid that TimeDiff == TimeDiff has to exist).
But operator> and operator< should. For example,
if (elapsed >= 0.1) // 100 ms or more elapsed?
...
> I just thought that I could also rename it to lowres_timer instead of
> precise_timer, it's more accurate, what do you think ?
Low resolution would mean it is less precise.
As you might have seen, I'd propose to use Time and TimeDiff.
The reason that I think that a 'Time' and 'TimeDiff' approach
is more useful than a 'HiResTimer', is that you can implement
the latter with the first, but not the other way around.
Boost should provide a portable interface for the lowest level.
Carlo Wood
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk