...

View Full Version : Speed of Switch vs If Else



bauhsoj
09-24-2005, 05:01 AM
I have read that "switch" statements are faster in JavaScript than "if, else if" statements. Does the same hold true in general for PHP?

missing-score
09-24-2005, 05:08 AM
I would imagine so... becuase with a switch, PHP compares an original value to a set of case values, the same holds with javascript... With an if else statement, it is given possibly different options each time and needs to do more parsing.

The differences here are going to literally un-noticable, and for general use, dont try and convert your if else statements to switch's unless a switch() loop is a more appropriate loop to use.

bauhsoj
09-25-2005, 04:28 AM
On basic comparisons it doesn't seem to make any difference between the two.

http://www.php.lt/benchmark/phpbench.php

missing-score
09-25-2005, 04:32 AM
Calling an if/else and switch statement 1000 times isnt anywhere near enough to get a good idea of speed difference... You would want to call it 1,000,000 - 100,000,000 times and check speed differences then, which would give a much better idea. I might try this out later actually.

Leppy
02-16-2009, 06:03 AM
On basic comparisons it doesn't seem to make any difference between the two.

http://www.php.lt/benchmark/phpbench.php

Very interesting, thanks!



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum