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

Colin Holgate coiin at verizon.net
Sun Feb 20 11:13:23 CST 2011


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:


on oneway
   put the milliseconds into t
   repeat with a = 1 to 10000
       dooneway
    end repeat
   put the milliseconds - t
end oneway

on otherway
   put the milliseconds into t
   repeat with a = 1 to 10000
       dootherway
    end repeat
   put the milliseconds - t
end otherway


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".

The results I got for working through 1000 entries, 10,000 times, was:

for each 1075mS
repeat with 1370mS
count array 4250

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



More information about the use-livecode mailing list