Re: [PHP] Re: 1 last error to fix before the application is done!
- From: aballard@xxxxxxxxx ("Andrew Ballard")
- Date: Tue, 14 Oct 2008 09:49:29 -0400
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
.
- Follow-Ups:
- Re: 1 last error to fix before the application is done!
- From: Colin Guthrie
- Re: 1 last error to fix before the application is done!
- References:
- 1 last error to fix before the application is done!
- From: Jason Pruim
- Re: [PHP] 1 last error to fix before the application is done!
- From: Jochem Maas
- Re: [PHP] 1 last error to fix before the application is done!
- From: Jason Pruim
- Re: [PHP] 1 last error to fix before the application is done!
- From: Yeti
- Re: 1 last error to fix before the application is done!
- From: Colin Guthrie
- 1 last error to fix before the application is done!
- Prev by Date: Re: The 'at' sign (@) variable prefix
- Next by Date: Re: 1 last error to fix before the application is done!
- Previous by thread: Re: 1 last error to fix before the application is done!
- Next by thread: Re: 1 last error to fix before the application is done!
- Index(es):
Relevant Pages
|