[REBOL] Handy Timer Functions Re:(3)
Larry wrote: > tic5: func [/local t] [ > t: now/time > toc5: func [] [print ["elapsed time:" now/time - t]] > ] This might be slightly more accurate/faster: tic6: func [/local t] [ toc6: func [] [print ["elapsed time:" now/time - t]] t: now/time ] I've just swapped the two lines. Andrew Martin [EMAIL PROTECTED] http://members.xoom.com/AndrewMartin/ Online @ 33,600 Baud! -><-
[REBOL] Handy Timer Functions Re:(2)
Hi Elan Thanks for the comment. I agree. Actually, I am experimenting with trying to implement Lisp (actually Scheme) programming techniques in REBOL. Capturing state in a function is one of the basic techniques emphasized in Structure and Interpretation of Computer Programs by Abelson and Sussman. I suspect you know this book. Anyway, I found a version of 'tic and 'toc that I like even better than version 3 below. It avoids the awkwardness of using 'compose and is easily generalized for more complex situations. It does not seem to be hurt by 'recycle. tic5: func [/local t] [ t: now/time toc5: func [] [print ["elapsed time:" now/time - t]] ] >> tic5 wait 5 recycle toc5 elapsed time: 0:00:05 What do you think? Larry - Original Message - From: <[EMAIL PROTECTED]> To: <[EMAIL PROTECTED]> Sent: Tuesday, December 28, 1999 5:50 PM Subject: [REBOL] Handy Timer Functions Re: > Hi Larry, > > first comment, I like your version 3 best: > > >tic3: func [] [toc3: func [] compose/deep [print ["elapsed time:" now/time - > >(now/time)]]] > > This approach really makes good use of REBOL's abilities. While it looks > trivial, I think that functions, which create functions, which in turn > reflect some state at the time the "dynamic" function was created is an > important technique for building smarter programs. In a different > application the state embedded in the created function coould very well be > more complicated and specific to the creating function then in your case. > Imagine a whole chain of function-creating functions. > > Elan >
[REBOL] Handy Timer Functions Re:(2)
Hi Jerry Yup, that works. But you could also nest timings with a simple change to tic3 and toc3. tic: func [] [insert [] now/time exit] toc: func [] [ print ["elapsed time:" now/time - first second second :tic] remove second second :tic exit ] >> tic tic wait 3 toc tic wait 5 toc tic wait 2 toc toc elapsed time: 0:00:03 elapsed time: 0:00:05 elapsed time: 0:00:02 elapsed time: 0:00:10 BTW you can see what's stored in tic with 'source: >> tic wait 2 tic >> source tic tic: func [][insert [18:01:45 18:01:43] now/time exit] Best Wishes Larry - Original Message - From: <[EMAIL PROTECTED]> To: <[EMAIL PROTECTED]> Sent: Tuesday, December 28, 1999 4:45 PM Subject: [REBOL] Handy Timer Functions Re: > Larry, > I like your tic toc pair. However I sometimes wish to nest their use as in > the example: > >> tic tic wait 3 toc tic wait 5 toc tic wait 2 toc toc > 0:00:03 > 0:00:05 > 0:00:02 > 0:00:10 > >> > The program I used is > stack: copy [] > push: func [x][insert stack x exit] > pop: func [/local a] > [ >if (length? stack) = 0 [Print "can't pop empty stack" exit] >a: first stack remove stack a > ] > tic: func [][ push now/time exit] > toc: func [][print now/time - pop exit] > > What do you think? > > Jerry >
[REBOL] Handy Timer Functions Re:
Hi Larry, first comment, I like your version 3 best: >tic3: func [] [toc3: func [] compose/deep [print ["elapsed time:" now/time - >(now/time)]]] This approach really makes good use of REBOL's abilities. While it looks trivial, I think that functions, which create functions, which in turn reflect some state at the time the "dynamic" function was created is an important technique for building smarter programs. In a different application the state embedded in the created function coould very well be more complicated and specific to the creating function then in your case. Imagine a whole chain of function-creating functions. Elan
[REBOL] Handy Timer Functions Re:
Larry, I like your tic toc pair. However I sometimes wish to nest their use as in the example: >> tic tic wait 3 toc tic wait 5 toc tic wait 2 toc toc 0:00:03 0:00:05 0:00:02 0:00:10 >> The program I used is stack: copy [] push: func [x][insert stack x exit] pop: func [/local a] [ if (length? stack) = 0 [Print "can't pop empty stack" exit] a: first stack remove stack a ] tic: func [][ push now/time exit] toc: func [][print now/time - pop exit] What do you think? Jerry
[REBOL] Handy Timer Functions
Hi all Below are some handy timer functions for REBOL. The idea of tic and toc comes from the MATLAB numeric package. One advantage of using a pair of functions is that they can be placed most anywhere in a piece of code without modifying it (as is sometimes necessary with the time-block approach. I provide three implementations which differ in how the initial time is stored. example usage: >> tic loop 100 [sine 30] toc elapsed time: 0:00:02 >> The first approach is plain vanilla; the initial time is stored in a global variable. To minimize name conflicts, I gave it an unusual name: __t__. (feel free to be creative with this name) tic1: func [] [__t__: now/time exit] toc1: func [] [print ["elapsed time:" now/time - __t__]] In the second approach, the initial time is stored in a literal block in the body of the tic function, and then retrieved by toc. tic2: func [] [change [] now/time exit] toc2: func [] [print ["elapsed time:" now/time - first second second :tic2]] In the third approach, the initial time is stored in the body of the toc function which is created by tic. tic3: func [] [toc3: func [] compose/deep [print ["elapsed time:" now/time - (now/time)]]] Just choose the one you like and rename to plain tic and toc. Comments welcome! Larry