if else vs switch
Moderators: FourthWorld, Klaus
if else vs switch
What is the relative performance difference of if / else versus switch statement in liveCode?
Re: if else vs switch
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.
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.
-
- VIP Livecode Opensource Backer
- Posts: 9848
- Joined: Sat Apr 08, 2006 7:05 am
- Location: Los Angeles
- Contact:
Re: if else vs switch
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.
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
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn
Re: if else vs switch
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.
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
Script Tracker https://github.com/bwmilby/scriptTracker
-
- Livecode Opensource Backer
- Posts: 9444
- Joined: Fri Feb 19, 2010 10:17 am
- Location: Bulgaria
Re: if else vs switch
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.
Perhaps there should be a FAQ section 'somewhere' to speed things up for folk with this sort of Q.
-
- VIP Livecode Opensource Backer
- Posts: 9731
- Joined: Wed May 06, 2009 2:28 pm
- Location: New York, NY
Re: if else vs switch
Hi.
I fooled around with this in a button script, with a field "x":
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
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
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
-
- VIP Livecode Opensource Backer
- Posts: 9848
- Joined: Sat Apr 08, 2006 7:05 am
- Location: Los Angeles
- Contact:
Re: if else vs switch
IIRC from the last time I benchmarked it, case statements with literals are faster than case statements with evaluations.
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn
-
- Livecode Opensource Backer
- Posts: 9444
- Joined: Fri Feb 19, 2010 10:17 am
- Location: Bulgaria
Re: if else vs switch
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.
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.
-
- VIP Livecode Opensource Backer
- Posts: 9848
- Joined: Sat Apr 08, 2006 7:05 am
- Location: Los Angeles
- Contact:
Re: if else vs switch
9000 cases seems like maybe a good job for an array lookup.
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn
-
- Livecode Opensource Backer
- Posts: 9444
- Joined: Fri Feb 19, 2010 10:17 am
- Location: Bulgaria
Re: if else vs switch
Indeed.FourthWorld wrote: ↑Mon Nov 02, 2020 11:31 am9000 cases seems like maybe a good job for an array lookup.
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.
Re: if else vs switch
Hello everyone and thank you very much for all your help.
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
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
thanks to all for the help.
here is an entry where we share the library we created.
viewtopic.php?f=15&t=35112
here is an entry where we share the library we created.
viewtopic.php?f=15&t=35112
-
- VIP Livecode Opensource Backer
- Posts: 9848
- Joined: Sat Apr 08, 2006 7:05 am
- Location: Los Angeles
- Contact:
Re: if else vs switch
Super, andresdt. Thanks for posting that.
So how did the performance measurements turn out?
So how did the performance measurements turn out?
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn
-
- Posts: 722
- Joined: Thu Sep 11, 2014 1:49 pm
- Location: The Netherlands
Re: if else vs switch
Thanks for posting
Re: if else vs switch
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.