man,  just swap the variable names and our example are almost identical...

;-)

I didn't use an object, just because I tought he really just wanted a function... it 
uses one global word for accumulated do times... it also safely returns a value, which 
can be cool if you also wish to test the validity of returned data in addition to 
speed gains.

for example, you could more easily trap floating point problems... when testing 
different floating point tricks, cause you can then compare the value...


-MAx
---
"You can either be part of the problem or part of the solution, but in the end, being 
part of the problem is much more fun."
 

> -----Original Message-----
> From: Joel Neely [mailto:[EMAIL PROTECTED]
> Sent: Wednesday, October 01, 2003 2:45 PM
> To: [EMAIL PROTECTED]
> Subject: [REBOL] Re: help with function utilising "extended 
> life-time" words ..
> 
> 
> 
> 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.
> 
> 
> -- 
> To unsubscribe from this list, just send an email to
> [EMAIL PROTECTED] with unsubscribe as the subject.
> 
> 

-- 
To unsubscribe from this list, just send an email to
[EMAIL PROTECTED] with unsubscribe as the subject.

Reply via email to