[RFC] a time interval class?
Marius Kruger
amanic at gmail.com
Wed Aug 15 06:22:12 BST 2007
hi,
this sounds cool but, ...
On 8/14/07, Robert Collins <robertc at robertcollins.net> wrote:
>
> We're doing interval calculations quite a bit now - with the smart
> server logging, benchmark tests. Looks to me like we can save some code
> by making a class to measure duration and report nicely formatted
> strings.
>
> something like
> >>> duration = bzrlib.osutils (or wheverever) .Duration()
> >>> duration.start()
> >>> time.sleep(0.01)
> >>> duration.stop()
> >>> print duration
> 0.01 seconds
> >>> duration.resume()
> >>> time.sleep(0.01)
> >>> duration.stop()
> >>> print duration
> 0.02 seconds
>
I've done this before in Java
and I went for a total and a delta timer.
Stopwatch sw = new Stopwatch(); //this automatically starts both timers
sw.getDelta(); //returns and resets the delta timer
sw.printDelta(); //prints and resets the delta timer
sw.printTotal(); //prints total time
sw.reset(); //resets both timers.
The only reason I mention this is IMO your api requires to many calls or
most uses.
I suggest then that you:
1) don't require a .start() (let the constructor start it automatically)
2) add another method .check() (which returns and resets your delta timer)
3) add another method .revert() (which returns and resets your delta
timer, but also reduces your total timer by the current delta)
then your usage will look like:
>>> duration = bzrlib.osutils (or wheverever) .Duration()
>>> time.sleep(0.01)
>>> print duration.check()
0.01 seconds
>>> duration.check() #check but don't print it (if you want to reset delta
timer, but still add this delta to the total)
>>> time.sleep(0.01)
>>> print duration.check()
0.01 seconds
>>> print duration.total()
0.02 seconds
>>> time.sleep(0.01)
>>> duration.revert() #reduce total with the current delta
>>> print duration.total()
0.02 seconds
thats better for a lazy coder like me.
it looks a bit crazy, so use what you like
hey you wanted comments
regards
marius
-------------- next part --------------
An HTML attachment was scrubbed...
URL: https://lists.ubuntu.com/archives/bazaar/attachments/20070815/84afccad/attachment.htm
More information about the bazaar
mailing list