[REBOL] Re: help with function utilising "extended life-time" words ..
From: joel:neely:fedex at: 1-Oct-2003 13:44
Hi, Petr,
Here are a couple of simple approaches (which you can complicate
as desired for more functionality ;-)
-jn-
Petr Krenzelok wrote:
> start: now/time/precise .... do something ... print now/time/precise -
> start start: now/time/precise
>
> I wanted to write myself short logger function, which will save me from
> repeating above sequences, as the script becomes a bit messy then. So I
> wanted to have following interface:
>
> >> how-long? "Some task ..."
>
Single-task timing doesn't require any tricky state:
how-long?: func [msg [string!] to-do [block!] /local timing] [
timing: now/time/precise
do to-do
timing: to-decimal now/time/precise - timing
print [timing msg]
]
Which behaves as in the following transcript:
(begin transcript)
>> how-long? "count to one million" [for i 1 1000000 1 []]
3.195 count to one million
>> how-long? "count to one hundred thousand ten times" [
[ loop 10 [
[ how-long? "count to one hundred thousand" [for i 1 100000 1[]]
[ ]
[ ]
0.33 count to one hundred thousand
0.321 count to one hundred thousand
0.31 count to one hundred thousand
0.321 count to one hundred thousand
0.31 count to one hundred thousand
0.321 count to one hundred thousand
0.32 count to one hundred thousand
0.321 count to one hundred thousand
0.31 count to one hundred thousand
0.32 count to one hundred thousand
3.184 count to one hundred thousand ten times
>>
(end transcript)
If you want accumulation of times across multiple calls, just
wrap the total in a block (per the tricky approach you quoted)
or do The Right Thing and represent stateful timing accumulators
with objects:
time-accumulator: make object! [
message: "no message?"
total: 0.0
reset: func [][total: 0.0]
time-this: func [to-do [block!] /local timing] [
timing: now/time/precise
do to-do
timing: to-decimal now/time/precise - timing
total: total + timing
print ["Total:" total "This:" timing message]
]
]
which can be used as follows:
>> stopwatch1: make time-accumulator [message: "Watch 1"]
>> stopwatch1/time-this [for i 1 1000000 1 []]
Total: 3.155 This: 3.155 Watch 1
>> stopwatch2: make time-accumulator [message: "smaller chunks"]
>> loop 10 [stopwatch2/time-this [for i 1 100000 1 []]]
Total: 0.32 This: 0.32 smaller chunks
Total: 0.631 This: 0.311 smaller chunks
Total: 0.941 This: 0.31 smaller chunks
Total: 1.262 This: 0.321 smaller chunks
Total: 1.582 This: 0.32 smaller chunks
Total: 1.893 This: 0.311 smaller chunks
Total: 2.203 This: 0.31 smaller chunks
Total: 2.514 This: 0.311 smaller chunks
Total: 2.834 This: 0.32 smaller chunks
Total: 3.144 This: 0.31 smaller chunks
>>
I prefer to represent stateful entities via objects rather than
functions. Among other reasons, stateful functions depend on aspects
of REBOL (persistence of mutations to "literal" series values) that
are very mysterious to REBOL newbies. There's no point in being
obscure, just for the sake of obscurity! ;-)
-jn-
--
----------------------------------------------------------------------
Joel Neely joelDOTneelyATfedexDOTcom 901-263-4446
Counting lines of code is to software development as
counting bricks is to urban development.