Page 1 of 1

if else vs switch

Posted: Wed Oct 28, 2020 9:47 pm
by andresdt
What is the relative performance difference of if / else versus switch statement in liveCode?

Re: if else vs switch

Posted: Wed Oct 28, 2020 10:25 pm
by Klaus
Hi andresdt,

welcome to the forum!

No idea what the performance difference may be, but most of the time a SWITCH statement
is definitively better readable than a lot of nested IF THENs.


Best

Klaus

P.S.
Personal note: A little "Hello" or something for the very first posting would not have hurt.

Re: if else vs switch

Posted: Wed Oct 28, 2020 10:55 pm
by FourthWorld
What Klaus said.

There is a measurable difference, but it's very, very minor, and varies broadly by the specifics of a given usage.

Coding for clarity is almost always the better priority.

Re: if else vs switch

Posted: Fri Oct 30, 2020 3:43 am
by bwmilby
There is some code in use for LCFM that will eventually make it over to the main LC product that will improve switch performance in some cases even more. Probably wouldn’t make a difference unless you had a large number of options to check in the switch though.

With that in mind, it probably would be worth the effort to use a switch where it makes sense.

Re: if else vs switch

Posted: Fri Oct 30, 2020 9:22 am
by richmond62
This is a question that comes round every year with the roast chestnuts.

Perhaps there should be a FAQ section 'somewhere' to speed things up for folk with this sort of Q.

Re: if else vs switch

Posted: Mon Nov 02, 2020 5:33 am
by dunbarx
Hi.

I fooled around with this in a button script, with a field "x":

Code: Select all

on mouseup
   put the ticks into tStart
   repeat 10000000
      put random(99) into temp
      switch
         case temp mod 2 = 0 
            break
         case temp mod 2 = 1 
            break
      end switch
   end repeat
   put the ticks - tStart into fld "x"
   
   put the ticks into tStart
   repeat 10000000
      put random(99) into temp
      if temp mod 2 = 0 then
      else if temp mod 2 = 1 then
      end if
   end repeat
   put the ticks - tStart into line 2 of fld "x"
end mouseup
The "switch" construction took 324 ticks, whereas the "if" construction took 313. "If" is 3.4% faster. But in absolute terms, only 11 ticks faster, and with ten million interations, in real world terms, the same.

I use "if" constructions when there are only a couple of "cases" to test, and switch if there are, say, three or more. "Switch" is SO much easier to read and manage with more than just a few "cases'.

On the other hand, with only, say, two "cases", "switch" seems unseemly. This is only a matter of style, however. They do the same job.

Craig

Re: if else vs switch

Posted: Mon Nov 02, 2020 8:33 am
by FourthWorld
IIRC from the last time I benchmarked it, case statements with literals are faster than case statements with evaluations.

Re: if else vs switch

Posted: Mon Nov 02, 2020 11:26 am
by richmond62
Switch . . . case saved my Devawriter Pro . . .

Am reminded of this as I am just starting my SDLC 5ish implementing a Devanagari to IAST automated transliterator
that is set to use about 9000 case statements . . .

. . . even if only in terms of repetitive stress injury, 9000 case statements sure beat the hell out of 9000 if . . . then statements.

Re: if else vs switch

Posted: Mon Nov 02, 2020 11:31 am
by FourthWorld
9000 cases seems like maybe a good job for an array lookup.

Re: if else vs switch

Posted: Mon Nov 02, 2020 11:50 am
by richmond62
FourthWorld wrote:
Mon Nov 02, 2020 11:31 am
9000 cases seems like maybe a good job for an array lookup.
Indeed.

BUT I can leverage existing code with an automated routine that takes 2 existing scripts (each about 5000 lines long)
and "magically" does a "great switcheroo" for me; so, in fact, apart from working out the initial automation routine (and
I have already 'pulled' several of those in my Devawriter development), all I am going to do is drink a large cup of
hot Carob while my Mac Mini does the "chewing" for me.

With the wisdom of hindsight (almost always with 100% clarity) the whole stack could have worked on array lookups, instead of
something close to 200,000 lines of code . . . the 'problem' in-so-much-as-it-may-be-seen-as-a-problem, is that my Devawriter started out
as a "quick-n-dirty" which, almost by itself, grew into a 'chunky, clunky and utterly filthy', and as well as this new interface I am
supposed (if, for once in my life am sufficiently self-disciplined) to refactor some of the 'chunk-n-clunk'.

I suspect that in an awful lot of computer work . . .

. . . c.f. some of those orphaned stacks buried away in the LiveCode IDE that might cause a bad case of appendicitis were they ever to come
up to the surface again . . .

. . . there is an awful lot of code that, with the wisdom of hindsight might have been put together differently and in a far, far
more efficient way.

--------

Teaching LiveCode I am constantly amazing myself by pointing out to kids how to do things more efficiently and
realising what a great, steaming hypocrite I am when stuff I wrote in LiveCode/RunRev 18-19-20 years ago looks
like a giant sloth that forgot to have its morning coffee. 8)

Re: if else vs switch

Posted: Sun Nov 15, 2020 10:35 pm
by andresdt
Hello everyone and thank you very much for all your help. :oops:

In my case I am trying to make a library for processing JSON that is as fast as possible. For which I am trying to use whatever allows me to gain a few milliseconds.
I KNOW THAT THERE ARE ALREADY SEVERAL WHO DO THIS.
But it is a personal challenge :)
Using dunbarx code I created something closer to what I need and the SWITCH was faster than IF-ELSE

Code: Select all

constant kNumRepeats = 100
on mouseup
   local tStart, temp, tJson
   put fld 2 into tJson
   
   put the milliseconds into tStart
   repeat kNumRepeats   
      tokensSwitch  tJson
   end repeat
   
   put the milliseconds - tStart into fld "x"

   put the milliseconds into tStart
   
   repeat kNumRepeats
      tokensIf tJson
   end repeat
   
   put the milliseconds - tStart into line 2 of fld "x"
end mouseup



command tokensIf pJson
   repeat for each token tToken in pJson
      if tToken is "{" then
         
      else if tToken is "[" then
         
      else if tToken is ":" then
         
      else if tToken is "," then
         
      else if tToken is "}" then
         
      else if tToken is "]" then
         
      else
         
      end if
   end repeat
end tokensIf


command tokensSwitch pJson
   repeat for each token tToken in pJson
      switch tToken
         case "{"
            
            break
         case "["
            
            break
         case ":"
            
            break
            
         case ","
            
            break
         case "}"
            
            break
         case "]"
            
            break
         default
            
      end Switch
      
      
   end repeat
   
end tokensSwitch


Re: if else vs switch

Posted: Tue Dec 15, 2020 12:20 am
by andresdt
thanks to all for the help.
here is an entry where we share the library we created.

viewtopic.php?f=15&t=35112

Re: if else vs switch

Posted: Tue Dec 15, 2020 12:31 am
by FourthWorld
Super, andresdt. Thanks for posting that.

So how did the performance measurements turn out?

Re: if else vs switch

Posted: Tue Dec 15, 2020 9:38 am
by mrcoollion
Thanks for posting :D

Re: if else vs switch

Posted: Tue Dec 15, 2020 2:52 pm
by andresdt
Thanks to the performance measurements, we were able to achieve that when converting a JSON into an array, the speed was very similar to that of Monte G's mergeJSON. In the github repository there is a stack to measure the performance of this library.