performance - PHP: Fastest way to handle undefined array key -


in tight loop need access tenthousands of values in array containing millions of elements. key can undefinied: in case shall legal return null without error message:

array key exists: return value of element. array key not exist: return null.

i know multiple solutions:

    if (isset($lookup_table[$key])) {         return $lookup_table[$key];     } else {         return;     } 

or

@return $lookup_table[$key]; 

or

error_reporting(0); $return = $lookup_table[$key]; error_reporting(e_all); return $return; 

all solutions far optimal:

  • the first 1 requires 2 lookup in b-tree: 1 check existence, retrieve value. doubles runtime.
  • the secondone uses error supression operator, , creates massive overhead on line.
  • the third 1 calls error handler (that check error_reporting setting , display nothing) , thereby creates overhead.

my question if miss way avoid error handling , yet work single btree lookup?

to answer questions:

the array caches results of complex calculation - complex done in real time. out of billions of possible values, millions yied valid result. array looks 1234567 => 23457, 1234999 => 74361, .... saved php-file of several megabyte, , include_once-d @ beginning of execution. initial load time not matter. if key not found, means specific calue not return valid result. trouble done 50k+ per second.

conclusion

as there no way found value single lookup , without error handling, hve trouble accepting single answer. instead upvoted great contributions.

the valuable inputs where: - use array_key_exists, faster alternatives - check out php's quickhash

there lot of confusion on how php handles arrays. if check sourcecode, see arrays balanced trees. building own lookup methods common in c , c++, not performant in higher script-languages php.

update

since php 7 can accomplish null coalesce operator:

return $table[$key] ?? null; 

old answer

first of all, arrays not implemented b-tree, it's hash table; array of buckets (indexed via hash function), each linked list of actual values (in case of hash collisions). means lookup times depend on how hash function has "spread" values across buckets, i.e. number of hash collisions important factor.

technically, statement correct:

return array_key_exists($key, $table) ? $table[$key] : null; 

this introduces function call , therefore much slower optimized isset(). how much? ~2e3 times slower.

next using reference avoid second lookup:

$tmp = &$lookup_table[$key];  return isset($tmp) ? $tmp : null; 

unfortunately, modifies original $lookup_table array if item not exist, because references made valid php.

that leaves following method, own:

return isset($lookup_table[$key]) ? $lookup_table[$key] : null; 

besides not having side effect of references, it's faster in runtime, when performing lookup twice.

you dividing arrays smaller pieces 1 way mitigate long lookup times.


Comments

Popular posts from this blog

blackberry 10 - how to add multiple markers on the google map just by url? -

php - guestbook returning database data to flash -

delphi - Dynamic file type icon -