switch case question

Mark Smith mark at maseurope.net
Fri Nov 3 14:14:27 EST 2006


Kay, I meant to check this out when I saw your post, but it's taken  
me a little time to get to it. I'm pretty sure that for multiple  
choice situations, a switch structure seems more efficient (ie.  
faster) than an equivalent multi-way if-then-else structure.

I'd be interested to see if you get similar results to mine if you  
paste the following into a button:

on mouseUp
   repeat 100000
     put random(6) & cr after rList
   end repeat

   put the millisecs into st

   repeat for each line L in rList
     if L = 1 then
       get 1
     else if L = 2 then
       get 2
     else if L = 3 then
       get 3
     else if L = 4 then
       get 4
     else if L = 5 then
       get 5
     else if L = 6 then
       get 6
     end if

     --    switch L
     --    case 1
     --      get 1
     --      break
     --    case 2
     --      get 2
     --      break
     --    case 3
     --      get 3
     --      break
     --    case 4
     --      get 4
     --      break
     --    case 5
     --      get 5
     --      break
     --    case 6
     --      get 6
     --      break
     --    end switch
   end repeat

   put the millisecs - st
end mouseUp

When I do this, (Mac 1.5Mhz G4 laptop), the if-then-else version  
takes 180-ish milliseconds, whereas the switch-case version takes 70- 
ish ms.
Admittedly, this isn't the difference between unusably slow and  
blisteringly fast, but it's always worth knowing your options....

Best,

Mark

On 31 Oct 2006, at 11:22, Kay C Lan wrote:

> I've come into this a little late, no internet for a week or so, but
> be aware that switch is slower than if-then-else-end if, no matter who
> many nested ifs are required to do 'the same' as a mult-case switch
> statement. It's only a couple of milliseconds over 1000 executions,
> but it is slower.




More information about the Use-livecode mailing list