How to use an array to solve the following...

Kay C Lan lan.kc.macmail at gmail.com
Sun Feb 19 23:50:33 EST 2012


On Mon, Feb 20, 2012 at 10:47 AM, Glen Bojsza <gbojsza at gmail.com> wrote:

>
> The biggest picture would be having 10 lists with each list having the xs
> column but a different ?t column (wt, gt, st, mt, qt, ... etc)
>
> If you are going to many more columns, then an array might be faster, and
if speed is the ultimate goal, then you would have to benchmark it to
compare, just to be sure.

For the repeat for each line approach, you basically have to process each
list, create the master list by combining and sorting, then do one final
run through the master list whilst confirming there are no repeat values
(left column number). Fortunately, because you are using 0, it would be
simply a matter of adding all the items of each repeated line together to
form a single line with all the correct values.

With arrays, time could be saved by not dealing with all the 0 values. ie
if you only create array elements for those that exist. If you later test
for an element, and it doesn't exist, the result is empty, which is
equivalent to 0.

The following is not verified so you would have to check the results for
accuracy. I've left a breakpoint at the end so you can inspect both tStore
which contains all the dummy data, and the final array aData. This should
give you a rough idea of how to proceed.

I used 76923 repeats of 13 lists so I got a total of 999999 cycles so I
could roughly compare to the other script. I got 5246ms but it must be
appreciated, this is doing a lot more than the previous script, even so,
it's pretty quick for a million cycles.

HTH

on mouseUp
   --create some dummy data
   put "at,bt,ct,dt,et,ft,gt,ht,it,jt,kt,lt,mt" into tHeader
   put 76923 into tRepeat
   repeat for each item tItem in tHeader
      put tItem & cr & 10 & tab & 5 & cr after tStore
      put 10 into tLast
      repeat tRepeat times
         put (10 * random(4) + tLast) into tLast
         put tLast & tab & random(30) & cr after tStore
      end repeat
   end repeat

   --work done here
   set the itemDelimiter to tab
   put the millisec into tStartTime
   repeat for each line tLine in tStore
      put item 1 of tLine into tCheck
      switch
         case (tCheck is not an integer)
            put tCheck into tElement
            --because of the way I created the list
            put 0 into tLastItem
            break
         case (tCheck - tLastItem > 20)
            put 0 into aData[(tLastItem + 10)][tElement]
            put 0 into aData[(tCheck - 10)][tElement]
            put item 2 of tLine into aData[tCheck][tElement]
            put tCheck into tLastItem
            break
         case (tCheck - tLastItem > 10)
            put 0 into aData[(tCheck - 10)][tElement]
            put item 2 of tLine into aData[tCheck][tElement]
            put tCheck into tLastItem
            break
         case (tCheck - tLastItem = 10)
            put item 2 of tLine into aData[tCheck][tElement]
            put tCheck into tLastItem
            break
      end switch
   end repeat
   put the millisec into tEndTime
   put (tEndTime - tStartTime) & "ms" & "  for " & (13 * tRepeat) & \
     " cycles." & cr into msg
   put "tStore:" & cr after msg
   put tStore after msg
   put cr & cr after msg
   breakpoint
end mouseUp



More information about the use-livecode mailing list