An interesting programming challenge

Geoff Canyon gcanyon at gmail.com
Fri Apr 10 15:47:29 EDT 2015


On Fri, Apr 10, 2015 at 12:02 PM, Peter Haworth <pete at lcsql.com> wrote:

> I don't think this is a fair comparison since the end results are totally
> different.  In the array test, the result is a large number of values
> whereas in the simple variable test you end up with one value.
>

I was emulating the original problem, where a single variable was being
updated in one handler vs. multiple values in an array in the other
handler. That's the key, rather than explicitly variables vs. arrays. So I
should have said that in the practical sense, if you can get away with
storing one value rather than multiple values, array or otherwise, you
should do so.


I did totally screw up in that I I used X[i] instead of X[it] forcing the
array to store all the values. The corrected version:

-- takes about 8 seconds:
on mouseUp
   put the long seconds into T
   repeat with i = 1 to 10000000
      get i mod 100
      add 23 to X[it]
   end repeat
   put the long seconds - T
end mouseUp

But back to variables vs. arrays. The following two admittedly awful
handlers compare variables vs. arrays fairly I think. Any difference
between storing one hundred separate variables and an array of one hundred
values gets lost in the noise of the gigantic switch statements:

-- ugly, takes about 5 seconds:
on mouseUp
   put the long seconds into T
   repeat with i = 1 to 2000000
      get i mod 100
      switch it
         case 0
            add 23 to X[0]
            break
         case 1
            add 23 to X[1]
            break
         case 2
            add 23 to X[2]
            break
         case 3
            add 23 to X[3]
            break

...

         case 97
            add 23 to X[97]
            break
         case 98
            add 23 to X[98]
            break
         case 99
            add 23 to X[99]
            break
      end switch
   end repeat
   put the long seconds - T
end mouseUp

-- uglier, also takes about 5 seconds:

on mouseUp
   put the long seconds into T
   repeat with i = 1 to 2000000
      get i mod 100
      switch it
         case 0
            add 23 to X0
            break
         case 1
            add 23 to X1
            break
         case 2
            add 23 to X2
            break
         case 3
            add 23 to X3
            break

...

         case 95
            add 23 to X95
            break
         case 96
            add 23 to X96
            break
         case 97
            add 23 to X97
            break
         case 98
            add 23 to X98
            break
         case 99
            add 23 to X99
            break
      end switch



More information about the use-livecode mailing list