Timing oddity

Geoff Canyon gcanyon at gmail.com
Sat Jan 25 18:18:35 EST 2014


Wow, the script editor's formatting came through poorly. The updated script
with no formatting is below. I realized that my earlier test had used
repeat for each key K in X, which as it turns out is faster than an index,
but not as fast as whole array math. So if X and Y are arrays,

add Y to X -- is fastest

repeat for each key K in X
    add Y[K] to X[K] -- is about 4 times slower
end repeat

repeat with i = 1 to maxIndex
    add Y[i] to X[i] -- is about 16 times slower
end repeat

Here are my results:

array size: 10

index: 1.097728

keys: 0.30297

whole array: 0.071286

index ratio: 15.398936

keys ratio: 4.250064



array size: 100

index: 1.149788

keys: 0.300583

whole array: 0.066067

index ratio: 17.403367

keys ratio: 4.549673



array size: 1000

index: 1.207281

keys: 0.30868

whole array: 0.069248

index ratio: 17.434176

keys ratio: 4.457602



array size: 10000

index: 1.210774

keys: 0.327856

whole array: 0.07695

index ratio: 15.734588

keys ratio: 4.260644



array size: 100000

index: 1.243161

keys: 0.332486

whole array: 0.08184

index ratio: 15.190132

keys ratio: 4.062634



array size: 1000000

index: 1.303055

keys: 0.354552

whole array: 0.09285

index ratio: 14.033985

keys ratio: 3.818548



Here is the script for anyone who wants to try:


on mouseUp

   repeat with arrayLog = 1 to 6

      put 10^arrayLog into arrayCount

      put 10^(6 - arrayLog) into repeatLoopCount

      repeat with i = 1 to arrayCount

         put random(100) into X[i]

         put random(10) into Y[i]

      end repeat

      put the long seconds into T

      repeat repeatLoopCount

         repeat with i = 1 to arrayCount

            add Y[i] to X[i]

         end repeat

      end repeat

      put the long seconds - T into T1

      put the long seconds into T

      repeat repeatLoopCount

         repeat for each key K in X

            add Y[K] to X[K]

         end repeat

      end repeat

      put the long seconds - T into T2

      put the long seconds into T

      repeat repeatLoopCount

         add Y to X

      end repeat

      put the long seconds - T into T3

      put "array size:" && arrayCount & cr & \

            "index:" && T1 & cr & \

            "keys:" && T2 & cr & \

            "whole array:" && T3 & cr & \

            "index ratio:" && T1 / T3 & cr & \

            "keys ratio:" && T2 / T3 & cr & cr & cr after R

      delete variable X

      delete variable Y

   end repeat

   put R

end mouseUp



More information about the use-livecode mailing list