switch case question

Dar Scott dsc at swcp.com
Sat Nov 4 12:53:05 EST 2006


On Nov 4, 2006, at 2:35 AM, Mark Smith wrote:

> This mirrors what I found, too. I'm at a loss as to why this might  
> be. I guess the conclusion has to be that if speed matters in a  
> particular application, then we must test things in context, and  
> find out which approach works best in context.

I ran some test with the same or similar 'if' and 'switch' and it  
seems to depend on the nature of L.


L                switch          if
random number     449ms         210ms
random numeral    391ms         518ms
random letter     204ms         266ms

The random number is 'random(6)'.

The random numeral is 'random(6)&empty'.

The random letter is 'numToChar( 64+random(6) )' and the switch/if  
cases adjusted to "A" to "F".  And the caseSensitive was set to true  
outside the loop.

It looks like switch is faster for strings.


That looked suspicious.  Sure enough, a number 6 will match a literal  
6 in a case, but a string "6.0" will not match a literal 6 in a  
case.  A string "6.0" will match a 6.0 case.  A number 6 will not  
match a 6.0 in the case.

A switch can only do a string match!

Dar




More information about the use-livecode mailing list