-- Colin Guthrie <[EMAIL PROTECTED]> wrote
(on Wednesday, 15 October 2008, 12:22 PM +0100):
> Karol Grecki wrote:
> >
> > Colin Guthrie-6 wrote:
> > > Having done some basic benchmarks, on this thread:
> > > http://thread.gmane.org/gmane.comp.php.general/190070
> > > the second example above gave me a ~3.5x speed increase. If there is
> > > a 2-depth array that needs checking then it yielded a ~4.5x speed
> > > increase.
> > >
> > > This would seem like a fairly easy "low hanging fruit" optimisation
> > > that could be made in the ZF with minimum of hassle.
> >
> > That's all good but if calls to array_key_exists() amount to ~0% of total
> > request time then 4x ~0% is still ~0%. In the meantime you can shed as much
> > as 10%, 20%... by removing actual bottlenecks. Optimisation without
> > profiling is just a waste of time and even if changes are easy to make, risk
> > of introducing bugs may not be worth it.
>
> Oh, indeed. My only check here was the number of calls to this contained
> in the ZF codebase and I didn't do any specific profiling on how often
> this actually comes into it.
>
> In my current use of ZF, we're only just beginning to roll it out into
> wider use, and to profile my use case would probably not be overly
> useful. I just think that the use of array_key_exists() vs.
> isset()/empty() is mostly a coding style thing (some people prefer one
> or the other or simply don't know the consequences), and by highlighting
> it here, hopefully less new calls can be added and some of the older,
> oft called instances can be converted to give a performance gain.
>
> I'm just making an observation here. People who are more involved in
> core development can judge whether or not it would make a difference :)
The problem is that there are situations where isset() and empty() are
not appropriate checks. For instance, try this:
$test = array('foo' => null);
echo ((isset($test['foo'])) ? 'true' : 'false'); // echoes 'false'!
What about empty()?
$test = array('foo' => null);
echo ((empty($test['foo'])) ? 'true' : 'false'); // echoes 'true'
echo ((empty($test['bar'])) ? 'true' : 'false'); // also echoes 'true'!
What does that mean? It means that if you want to test for the presence
of a key, you can't rely on isset() *or* empty()! array_key_exists() is
the only way to reliably verify that the key is present in the array.
There may be areas where we could switch to using one or the other...
but as somebody else pointed out, these would be micro-optimizations.
It's better to spend our time working on the true bottlenecks -- class
loading, the dispatch loop, etc.
--
Matthew Weier O'Phinney
Software Architect | [EMAIL PROTECTED]
Zend Framework | http://framework.zend.com/