tags:

views:

620

answers:

8

I was wondering which was better:

$lookup = array( "a" => 1, "b" => 2, "c" => 3 );
return $lookup[$key];

or

if ( $key == "a" ) return 1
else if ( $key == "b" ) return 2
else if ( $key == "c" ) return 3

or maybe just a nice switch...

switch($key){
case "a": return 1;
case "b": return 2;
case "c": return 3;
}

I always prefer the first method as I can separate the data from the code; At this scale it looks quite silly but on a larger scale with thousands of lines of lookup entries; How much longer is PHP going to take building an array and then only checking maybe 1 or 2 entries per request.

I think it'd have to be tested and clocked, but I'd say the bigger and more complicated the array the slower it's going to become.

PHP Should be able to handle lookups faster than I can in PHP-code, but building the array in the first place surely takes up a lot of time.

+2  A: 

There will be a tipping point you will just have to test to find it. My guess is with 3 items you are better off with if/then/else. This is a nice article on bit counting which compared computing the number of bits and using lookups. Spoiler: Lookups won!

joegtp
+4  A: 

For anything with measurable performance (not only 3 entries) lookup is fastest way. That's what hash tables are for.

Dev er dev
Using a hash table instead of an array for lookup is an important optimization.
Svante
Yes. And all arrays in PHP are hash tables.
gnud
+1  A: 

Are you building the array every time, or can you build it once and cache it?

If you are building it every time, I cannot see how that could be faster. Building the array by itself should take longer that the chained if()s (Adding one item to the array would be close in time to one if(), but you'd have to add every item, when you could exit from the if() early)

If you can use a cached array, than I think that would be the clear winner.

James Curran
Yeah, each request falls through a bunch of PHP files and the monolithic array is built every time. It's actually only built when it's needed but when it is needed its a lot of array building to get a small amount of data from.
widgisoft
If it's really a huge array, maybe you should move it to a database? Even a sqlite db with the serialized php values would probably speed things up, if you index the key and the data file is large enough.
gnud
+3  A: 

First, it's easy to test it yourself.

Second, and more importantly, which is most appropriate for the code you're using? The amount of time you'll save is negligible in any case.

Andy Lester
A: 

If you've got thousands of entries, an array lookup will win hands down. The associative array might be a bit slow, but finding an array key is much faster than doing thousands of if() blocks (not to mention the time it takes to type it all out!)

Ant P.
A: 

So I did a bit of testing with this example and got the following results:

emptyfunction:  0.00000087601416110992430969503855231472755349386716
lookuparray:    0.00000136602194309234629100648257538086483009465155
makearrayonly:  0.00000156002373695373539708814922266633118397294311
makearray:      0.00000174602739810943597796187489595842734502184612
ifblock:        0.00000127001986503601083772739543942265072473674081
switchblock:    0.00000131001937389373773757957151314679222764425504

Each was inside a method, so I also included the time for an empty method. They were ran 1,000,000 times each and then averaged out.

Just doing a lookup (without the building of the array) is actually slower than an if block (uses a global lookup the same as my code) and just by a fraction slower than a switch block.

I can't be bothered scaling this up to hundreds of if statements but it just shows that the if statement is faster even at this level against a single lookup.

widgisoft
As a side note, the if block I used was the second in the list versus the second in the hash lookup; Changing this to be the third item the lookup is marginally slower, so for items deeper down the list the lookup is going to be faster.
widgisoft
A: 

You could test how long it takes to see if the value is not there as well. My guess is the array key lookup will be faster. And if you have to query it twice or more, caching the array should make it faster.

But speed is not the most important thing here. The array key is better for the long term, where you want to add and remove data. You can also get the data from another source in the future.

  • If you just want to look up a value you use an array.
  • If you want to take an action then if and switch both have their uses.
OIS
A: 

hey all..this is a little test for array manipulations...
{$x=0; foreach($test as $k=>$v) {$x=sprintf(”%s=>%sn”,$k,$v);} } {$x=0; reset($test); while (list($k, $v) = each($test)) {$x=sprintf(”%s=>%sn”,$k,$v); } } {$x=0; $k=array_keys($test); $co=sizeof($k); for($it=0;$it%sn”,$k[$it],$test[$k[$it]]); } } {$x=0; reset($test); while ($k=key($test)) { $x=sprintf(”%s=>%sn”,$k,current($test)); next($test); } }

access time (ms) 8.1222 10.3221 9.7921 8.9711

nice day