News Feed
Sections




News Archive
Looking for more information on how to do PHP the right way? Check out PHP: The Right Way

Rob Thompson's Blog:
Switch vs. If
February 01, 2008 @ 14:28:00

Rob Thompson has posted about some simple benchmarking he did comparing the speed of a series of "if" statements versus one "switch" with multiple cases.

I got curious about which is actually more efficient at matching a random integer with a set of conditionals. So, I setup a script to create a set of large scripts to test the speed of these different constructs. Using the 'time' command, I measured the speed at which the condition could match a random number.

His results found that the switch statement is generally more than 2 times as fast at matching a simple integer. His tests, however, didn't go through much more than this simple test. It'd be interesting to see what it would do with something more complex (like longer strings or handling the result of an evaluation inside the definition of the switch().

0 comments voice your opinion now!
switch if evaluate benchmark elseif compare


blog comments powered by Disqus

Similar Posts

Greg Freeman: Your PHP Framework Choice doesn’t Matter

Larry Garfield's Blog: Benchmarking magic

Ask About PHP: PHP's alternative syntax and why it’s useful

David Sklar's Blog: Visiting each character in a string

Webification.com: Roundup of 17 Best PHP Template Engines


Community Events

Don't see your event here?
Let us know!


podcast voicesoftheelephpant laravel5 extension laravel community library interview language opinion symfony introduction unittest threedevsandamaybe series version security api release framework

All content copyright, 2015 PHPDeveloper.org :: info@phpdeveloper.org - Powered by the Solar PHP Framework