/    Sign up×
Community /Pin to ProfileBookmark

Switch or If-else?

Speed-wise, which computes quicker? When I looked it up, it seemed it could go both ways, in some cases a switch statement would be quicker, in other they would be basically the same. I know a switch is faster in c++ but is tha the same in php usually?

to post a comment
PHP

8 Comments(s)

Copy linkTweet thisAlerts:
@ArthurKayNov 02.2007 — I guess it would depend on how many cases you have to test...

If you only had 2 cases, I believe an If/Else statement would perform "faster" because there would be fewer lines of code. But, assuming you had X number of cases, the Switch would probably have fewer CPU statements to process.

It may also depend on the language you program in... some languages (like Python) don't require you to end your statements, since they are built on indentation. Come to think of it, I'm not sure Python uses Switch statements...
Copy linkTweet thisAlerts:
@bokehNov 02.2007 — It's an apples for oranges comparison and there are things much more important than speed to consider. Unless you have a very good understanding of equality comparisons between different variable types I would steer well clear of the switch statement because it doesn't allow identicality comparisons.
Copy linkTweet thisAlerts:
@NogDogNov 02.2007 — Some tests I ran a while back indicated that the difference was pretty small, with a slight advantage going to if/else. But we're talking about a few microseconds here; so unless you are working on a realtime missile tracking system or such where every microsecond counts, I would opt for whichever method is the most logical and easiest to maintain for a given situation.
Copy linkTweet thisAlerts:
@bokehNov 02.2007 — unless you are working on a realtime missile tracking system or such where every microsecond counts[/QUOTE]Even at Mach 10 (7600 mph), 1 microsecond is only 0.125 of an inch in distance, so a 10 foot long missile would take a whole millisecond to travel its own length.
Copy linkTweet thisAlerts:
@NogDogNov 02.2007 — Sure, a single difference in two programs of a few microseconds probably won't matter much for most things. However, if you're talking about a complex program (let's say something like the Aegis anti-air fire control system that needs to track multiple targets while simultaneously analyzing threat ratings, allocating and controlling defensive missile/gun assets, etc) that may consist of tens of thousands of lines of code, saving a few microseconds each in hundreds or thousands of places in the program code may be important.

But I digress.
Copy linkTweet thisAlerts:
@bokehNov 03.2007 — Aegis anti-air fire control system that needs to track multiple targets while simultaneously analyzing threat ratings, allocating and controlling defensive missile/gun assets[/QUOTE]That doesn't sound a very likely candidate for PHP.
Copy linkTweet thisAlerts:
@NogDogNov 03.2007 — That doesn't sound a very likely candidate for PHP.[/QUOTE]
Don't think I ever said it was. (Aegis software -- or at least parts of it -- were written CMS-2 back when I was vaguely associated with it. I have no idea what they are using now.) Which is why I originally said that for most purposes worrying about a few microseconds difference between switch or if/else is a non-issue, and was trying to simply make a point that while there [i]are[/i] some situations where such things are important, most of the time for us peons working on typical web sites, code readability/maintainability will be much more important than saving a microsecond or two here or there.

But go ahead and continue to nit-pick, I know how much you enjoy it. :p
Copy linkTweet thisAlerts:
@bokehNov 03.2007 — Don't think I ever said it was.[/QUOTE]I wasn't implying that you did. What I am getting at is that if someone were bothered about a couple of microseconds they wouldn't have chosen PHP to start with. Personally I find these "what's fastest" questions a bit of a drag. They seem to come up all the time: double quotes/single quotes; procedural/OOP; They put all the emphasis on speed and none on any of the important considerations.
×

Success!

Help @bejitto101 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.21,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...