[dev] Re: [cvs] commit: horde/lib String.php

Jan Schneider jan at horde.org
Mon Jan 27 10:56:36 PST 2003


Zitat von Michael M Slusarz <slusarz at bigworm.colorado.edu>:

> Quoting Chuck Hagenbuch <chuck at horde.org>:
> 
> | Quoting Jan Schneider <jan at horde.org>:
> |
> | > Please revert this.
> | > I recognized during a performance audit that this function was a real
> | > performance hit. Chuck and me also tested the performance of
> | > array_key_exist() vs. isset() and detected that isset() is around
> twice
> | > as fast. Unless there is really a reason to use array_key_exists(),
> we
> | > should in the future use isset() again.
> |
> | I agree with Jan, fwiw - I hadn't bothered to test this before, but you
> | can look at http://marina.horde.org/wip/isset.php and
> | http://marina.horde.org/wip/isset.phps (please copy and confirm
> locally).
> 
> My numbers from this script work out to be that isset() is approximately
> 25% faster that array_key_exists().  So you are right about that
> (although
> I am not getting anywhere near 100% performance increases like you and/or
> Chuck).
> 
> Here's the catch though.  isset() returns false if the value is set to
> null.  Thus, the following script:

That's why I said: "Unless there is really a reason."
I knew there would be ones, I just couldn't remember. ;-)
 
> Additionally, the following code:
> 
> -----
> $a = 'Some text.';
> if (isset($a['hi_i_am_a_fake_index'])) {
>   echo 'HI';
> }
> -----
> 
> amazingly enough outputs HI.  I realize this is bad coding, but this
> might
> be a situation where a bug would be difficult to track down - especially
> if
> the variable was defined elsewhere.  Once again, this is a case where I
> personally would expect isset() to return the opposite of what it
> actually
> does.

Much more interesting would be to know, _what_ $a['hi_i_am_a_fake_index']
actually returns. I guess the string is casted to 1 and 'o' would be
returned. I agreed that it would be bad coding if you dont't know what data
type you can expect but we already check with is_array() in some case to be
sure. Though I don't know what performance impact this might have.
 
> In both cases, array_key_exists() correctly handles these cases
> (correctly
> in the sense that it will return what you think it should return...
> whatever that means).

Right.
 
> Thus the argument to keep array_key_exists() is to ensure correct
> handling
> of this kind of situation Horde-wide.  It doesn't really matter to me -
> but
> these were the reasons I orginally wanted to switch to
> array_key_exists().

Thanks for making this clear. I hope we now always know when to use isset()
and when array_key_exists().

This might sound like a academic discussion as 25% or even 100% performance
increase isn't _that_ much, but I discovered this when I found out that
array_key_exists() was called about 1900 times while opening imp/folders.php
and cosumed about 20% of the scripts time.

Jan.

--
http://www.horde.org - The Horde Project
http://www.ammma.de - discover your knowledge
http://www.tip4all.de - Deine private Tippgemeinschaft


More information about the dev mailing list