if else vs switch

Stop by to discuss use cases, requirements, information architecture, flow diagraming, unit testing and usability.

Moderators: Klaus, FourthWorld

Post Reply
andresdt
Posts: 4
Joined: Fri Aug 16, 2019 7:51 pm

if else vs switch

Post by andresdt » Wed Oct 28, 2020 9:47 pm

What is the relative performance difference of if / else versus switch statement in liveCode?

Klaus
Posts: 12083
Joined: Sat Apr 08, 2006 8:41 am
Location: Germany
Contact:

Re: if else vs switch

Post by Klaus » Wed Oct 28, 2020 10:25 pm

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.

FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 8169
Joined: Sat Apr 08, 2006 7:05 am
Location: Los Angeles
Contact:

Re: if else vs switch

Post by FourthWorld » Wed Oct 28, 2020 10:55 pm

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.
Richard Gaskin
Community volunteer LiveCode Community Liaison

LiveCode development, training, and consulting services: Fourth World Systems: http://FourthWorld.com
LiveCode User Group on Facebook : http://FaceBook.com/groups/LiveCodeUsers/

bwmilby
Posts: 351
Joined: Wed Jun 07, 2017 5:37 am
Location: Henrico, VA
Contact:

Re: if else vs switch

Post by bwmilby » Fri Oct 30, 2020 3:43 am

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.
Brian Milby

Script Tracker https://github.com/bwmilby/scriptTracker

richmond62
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 5064
Joined: Fri Feb 19, 2010 10:17 am
Location: Bulgaria

Re: if else vs switch

Post by richmond62 » Fri Oct 30, 2020 9:22 am

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.

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 7168
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: if else vs switch

Post by dunbarx » Mon Nov 02, 2020 5:33 am

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

FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 8169
Joined: Sat Apr 08, 2006 7:05 am
Location: Los Angeles
Contact:

Re: if else vs switch

Post by FourthWorld » Mon Nov 02, 2020 8:33 am

IIRC from the last time I benchmarked it, case statements with literals are faster than case statements with evaluations.
Richard Gaskin
Community volunteer LiveCode Community Liaison

LiveCode development, training, and consulting services: Fourth World Systems: http://FourthWorld.com
LiveCode User Group on Facebook : http://FaceBook.com/groups/LiveCodeUsers/

richmond62
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 5064
Joined: Fri Feb 19, 2010 10:17 am
Location: Bulgaria

Re: if else vs switch

Post by richmond62 » Mon Nov 02, 2020 11:26 am

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.

FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 8169
Joined: Sat Apr 08, 2006 7:05 am
Location: Los Angeles
Contact:

Re: if else vs switch

Post by FourthWorld » Mon Nov 02, 2020 11:31 am

9000 cases seems like maybe a good job for an array lookup.
Richard Gaskin
Community volunteer LiveCode Community Liaison

LiveCode development, training, and consulting services: Fourth World Systems: http://FourthWorld.com
LiveCode User Group on Facebook : http://FaceBook.com/groups/LiveCodeUsers/

richmond62
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 5064
Joined: Fri Feb 19, 2010 10:17 am
Location: Bulgaria

Re: if else vs switch

Post by richmond62 » Mon Nov 02, 2020 11:50 am

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)

andresdt
Posts: 4
Joined: Fri Aug 16, 2019 7:51 pm

Re: if else vs switch

Post by andresdt » Sun Nov 15, 2020 10:35 pm

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


Post Reply

Return to “Software Engineering”