1...">

Which is faster, an array lookup (including assembling the array) or an IF stack?

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 because I can separate the data from the code; It looks pretty silly at this scale, but on a larger scale with thousands of rows of search entries; How much longer does PHP take to build an array and then check maybe 1 or 2 records per request.

I think it needs to be tested and synchronized, but I would say that the larger and more complex the array, the slower it will become.

PHP Should be able to process requests faster than I can in PHP code, but creating the array in the first place surely takes a long time.

+1


source to share


8 answers


For anything with measurable performance (not just 3 inputs), searching is the fastest way. What are hash tables for.



+6


source


First, it's easy to test it yourself.



Second, and more importantly, what is the best fit for the code you are using? In any case, the amount of time you will save is negligible.

+3


source


There will be a tipping point that you will need to check to find it. I guess with 3 articles you are better off with if / then / else. This is a good article on bit counting that compared bit counting and lookup usage. Spoiler alert: Search won!

+2


source


Do you create the array every time, or can you build it once and cache it?

If you build it every time I don't see how it could be faster. Building the array by itself should take longer if chaining if () s (adding one element to the array will be close in time to one if (), but you will need to add each element when you can exit the if () early)

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

+1


source


So, I tested this example a bit and got the following results:

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

      

They were each inside a method, so I included the time for the empty method as well. They ran 1,000,000 times each and then averaged.

Just searching (without building the array) is actually slower than the if block (uses a global search just like my code), and just a smaller fraction than the switch block.

I can't bother scaling this to hundreds of if statements, but it just shows that the if statement is faster even at this level versus a single lookup.

+1


source


If you have thousands of records, an array search will win. An associative array can be a little slow, but finding an array key is much faster than thousands of blocks if()

(not to mention the time it takes to enter it)

0


source


You can check how long it takes to find out if the value doesn't exist. My guess is finding the array key will be faster. And if you need to request it twice or more, array caching should make it faster.

But speed is not the most important thing here. An array key is best in the long run when you want to add and remove data. You can also get data from another source in the future.

  • If you just want to see the value you use an array.
  • If you want to take action if both switches have their uses.
0


source


hey everyone .. this is a little test for manipulating arrays ...
 {$ 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 ($ tests); $ Co = SizeOf ($ k); for ($ this = 0; $ this is% Sn ", $ to [$ this], $ test [$ to [$ this]]);}} {$ 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

a good day

0


source







All Articles