Re: [PHP] Re: 1 last error to fix before the application is done!



On Tue, Oct 14, 2008 at 8:52 AM, Colin Guthrie <gmane@xxxxxxxxxxxxxx> wrote:
Yeti wrote:

You might also want to try array_key_exists

if (array_key_exists('loggedin', $_SESSION['userInfo'])) {
// do something with $_SESSION['userInfo']['loggedin']
}

You'd first need to check that the key 'userInfo' existed in the $_SESSION
array too.

Personally, I very rarely see the point in using array_key_exists... It's a
function call and has overhead where as isset() and empty() are language
constructs and (I would hope) are much more efficient (although I've not
done any benchmarks).


I've heard that a lot, but I just don't see it. I'm sure some of you
can come up with better tests than this, but here is what I used:

<?php

// Load some big arrays
$x = get_defined_functions();

$x = $x['internal'];

$x += get_defined_constants();


$iterations = 50000;


for ($i = 0, $start = microtime(true); $i < $iterations; ++$i) {
isset($x[rand()]);
}
$elapsed = microtime(true) - $start;
$avg = $elapsed / $iterations;

echo "Completed $iterations iterations using isset() in $elapsed seconds.\n";
echo "Average time per function call: $avg\n\n";


for ($i = 0, $start = microtime(true); $i < $iterations; ++$i) {
empty($x[rand()]);
}
$elapsed = microtime(true) - $start;
$avg = $elapsed / $iterations;

echo "Completed $iterations iterations using empty() in $elapsed seconds.\n";
echo "Average time per function call: $avg\n\n";



for ($i = 0, $start = microtime(true); $i < $iterations; ++$i) {
array_key_exists(rand(), $x);
}
$elapsed = microtime(true) - $start;
$avg = $elapsed / $iterations;

echo "Completed $iterations iterations using array_key_exists() in
$elapsed seconds.\n";
echo "Average time per function call: $avg\n\n";


?>

Sample Results:
Completed 50000 iterations using isset() in 1.6928939819336 seconds.
Average time per function call: 3.3857879638672E-005

Completed 50000 iterations using empty() in 1.6825141906738 seconds.
Average time per function call: 3.3650283813477E-005

Completed 50000 iterations using array_key_exists() in 1.7125430107117 seconds.
Average time per function call: 3.4250860214233E-005

Based on these results, I'd hardly use the "language construct versus
function call" optimization argument to make my decision. I'm not sure
if this is a testament to improvements in the PHP engine over the last
couple years, or if equipment has gotten fast enough that the
differences have become irrelevant. Quite possibly, it's both.

Andrew
.



Relevant Pages

  • Re: Loop Efficiency
    ... although the array is untouched. ... Algo 1 took 24828ms for 10000 iterations ... private void makeData{ ...
    (comp.lang.java.programmer)
  • Re: Are your tiles really just tiles?
    ... One of my first prototype iterations used an X * Y grid of ... "tile" objects that could hold living beings, object, traps, etc. ... I would still store a 2D array of integers that are indices to the terrain ...
    (rec.games.roguelike.development)
  • [PATCH 19/25] lmb: Add array resizing support
    ... When one of the array gets full, we resize it. ... a few iterations of that code, I went back to on-demand resizing using ...
    (Linux-Kernel)
  • Re: Faster than Array search?
    ... If you know something about function1, it might be possible to do some ... reading an array of chars as many times. ... if it's the array lookup that costs, and not the computation of function2. ... to avoid doing some of the loop iterations. ...
    (comp.lang.java.programmer)
  • Re: OT: Different types of counting in loops
    ... which will do 5 or 6 iterations. ... if a is set to point to an array; ... portability which Fortran had 2 decades earlier. ... subscript checking, you can subscript one array with some out of range ...
    (comp.lang.fortran)