note 110232 deleted from language.types.array by cmb
| From: | cmb@php.net | Date: | Fri, 15 May 2015 18:44:58 +0000 |
| Subject: | note 110232 deleted from language.types.array by cmb | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-202534@lists.php.net to get a copy of this message | ||
Note Submitter: cromney at pointslope dot com
----
One thing to be careful of is making any assumptions about the underlying implementation with
respect to performance. For example, the documentation talks about hash-maps, which might lead you
to expect O(1) key lookups.
<?php
function find_val($n) {
$t = array();
$last = null;
for ($x = 0; $x < $n; $x++) {
$last = "" . $x;
$t[] = $last;
}
var_dump(in_array($last, $t));
}
function find_key($n) {
$t = array();
$last = null;
for ($x = 0; $x < $n; $x++) {
$last = "" . $x;
$t[$last] = true;
}
var_dump(array_key_exists($last, $t));
}
$n = 1600000;
find_val($n);
// Time taken: 1123ms
find_key($n);
// Time taken: 803
/*
Additional Timings:
n find_val(ms) find_key(ms)
100000 99 82
200000 169 130
400000 301 217
800000 570 416
1600000 1123 803
*/
?>
In my tests, both in_array and array_key_exists exhibited the same order of growth.