Efficient code for accessing the items (or lines) in a variable.

Alex Tweedly alex at tweedly.net
Sun Feb 20 20:29:56 EST 2011

On 20/02/2011 17:13, Colin Holgate wrote:
> I just did some timing tests, partly to see if using an array might help with speed. It didn't, but in doing the test I thought of a neater way of testing than I've done before.
> Usually my testing would resemble this:
> ...
> Then I would type in "oneway" or "otherway" in the message box. It works well enough, but if I decide to change how many repeats to do I have to go in and change each place. There's also a lot of same lines, where I'm making note of the time.
> What I did just now is below, and to test the various technique I just type "timeit technique,repeatcount" in the message box. So for example, to test the "for each" way of working through a set of items, and I want it to do enough that I get a good average, I would type "timeit foreach,10000".

Nice infrastructure for timing tests. Betraying my lack of HC 
background, I never the the msg box for this, but rather I have a test 
stack with one button and one field, and the butotn's mouseUp handler 
does the whole test for me :-)

The results I got for working through 1000 entries, 10,000 times, was:
> for each 1075mS
> repeat with 1370mS
> count array 4250
I think those times are irrelevant. To make them meaningful, you need to 
actually *use* the item within the loop, otherwise you don't see the 
effects of any efficiency gain or loss. I added the highlighted lines 
below, and then re-ran your tests

                     as-is     using item
> foreach        85         159
> repeatwith   93       3102
> countarray 276         367

> global someitems,anarray,t
> on fillitems
>     put empty into someitems
>     repeat with a = 1 to 1000
>        put random(100) into item a of someitems
>     end repeat
>     put someitems into anarray
>     split anarray by the itemdelimiter
> end fillitems
> on timeit what,howmany
>     put the milliseconds into t
>     repeat with a = 1 to howmany
>        do what
>     end repeat
>     put the milliseconds - t
> end timeit
> on foreach
>    repeat for each item tItem in someitems
>    end repeat
> end foreach
> on repeatwith
>      repeat with i = 1 to the number of items in someitems
>      end repeat
> end repeatwith
> on countarray
>       repeat for each element tElement in anarray
>       end repeat
> end countarray
> _______________________________________________
> use-livecode mailing list
> use-livecode at lists.runrev.com
> Please visit this url to subscribe, unsubscribe and manage your subscription preferences:
> http://lists.runrev.com/mailman/listinfo/use-livecode

More information about the Use-livecode mailing list