Best Practices in Rev development

Brian Yennie briany at qldlearning.com
Thu Jun 21 15:00:42 EDT 2007


Jacque,

I think what you are discovering is that "it depends". I noticed a  
couple of things with your example:

1) Your switch conditions are all expressions that need to be  
reevaluated in every iteration (as opposed to constant value). This  
effectively kills the switch statement's ability to "jump" to the  
correct result - since it has to reevaluate the "jumps" every time  
through. Try throwing in string constants and switch should start  
gaining.

2) 3-4 possible results is probably around the break-even point (in  
general). But every extra condition will typically cause a gain on  
the switch side.

I think the basic lesson though is that there is no general answer.  
It depends a lot on your data. For example, even the same if  
statement can be written to be 10 times faster or slower depending on  
the data. Suppose your first if statement passes 99% of the time, as  
opposed to failing all tests 99% of the time.

My general thoughts:

* Use switch statements when you have 3 or more constant expressions  
(or may need to add more in the future)
* Use switch statements if you need the "fall through" logic (by  
omitting break statements). Sometimes this leads to more elegant  
looking code.
* Use if statements when you have complex expressions and can predict  
to some degree which conditions will pass/fail most of the time
* If speed is not an issue, use whichever one makes the most sense to  
you

> 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
> _______________________________________________
> use-revolution mailing list
> use-revolution at lists.runrev.com
> Please visit this url to subscribe, unsubscribe and manage your  
> subscription preferences:
> http://lists.runrev.com/mailman/listinfo/use-revolution




More information about the use-livecode mailing list