Best Practices in Rev development

J. Landman Gay jacque at hyperactivesw.com
Thu Jun 21 14:22:28 EDT 2007


Scott Kane wrote:
> ----- Original Message ----- From: "J. Landman Gay" 
> <jacque at hyperactivesw.com>
> 
>> I read somewhere a long time ago that switch statements run faster 
>> than if/else. I think it was a general comment rather than 
>> specifically related to Revolution programming but it was so long ago 
>> I can't recall. Does anyone know if it is true for Rev scripts?
> 
> I haven't timed it - but if it's read by the engine in the same manner 
> other programming languages handle it then it would be faster as any 
> subsequent conditions are skipped because once the case statement 
> (switch statement) is met the compiler knows not to proceed - as opposed 
> to if/then/else etc where the entire handler is evaluated in full.

I remember reading that Rev doesn't evaluate the entire if/else, so I'd 
think it would be about the same as switch. But lookee here, I did a 
test, with surprising results:

on mouseUp
   put 100000 into tRepeats
   put the milliseconds into tstart
   switchTest tRepeats
   put the milliseconds - tstart into item 1 of tTimes
   put the milliseconds into tstart
   ifTest tRepeats
   put the milliseconds - tstart into item 2 of tTimes
   put tTimes
end mouseUp

on switchTest pRepeats
   put 1 into tRep
   repeat pRepeats
     switch tRep
     case tRep > 10000
       put tRep into x
       break
     case tRep > 5000
       put tRep into x
       break
     case tRep > 2000
       put tRep into x
       break
     default
       put tRep into x
     end switch
     add 1 to tRep
   end repeat
end switchTest

on ifTest pRepeats
   put 1 into tRep
   repeat pRepeats
     if tRep > 10000 then
       put tRep into x
     else if tRep > 5000 then
       put tRep into x
     else if tRep > 2000 then
       put tRep into x
     else
       put tRep into x
     end if
     add 1 to tRep
   end repeat
end ifTest


The "if/else" is 4 to 5 times faster. <blink> Is my test accurate?

-- 
Jacqueline Landman Gay         |     jacque at hyperactivesw.com
HyperActive Software           |     http://www.hyperactivesw.com



More information about the use-livecode mailing list