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

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Yeti wrote:
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 don't know what's wrong with this ..

$foo = array();
$foo['bar'] = null;
if (array_key_exists('bar', $foo)) {
 switch ($foo['bar']) {
  case true:
   //do something
   break 1;
  default:
   //do something else
}
}

# if that's causing more overhead then let me see your benchmark, then
i believe it

I don't really get what you're trying to say here.

All I'm saying is that empty(), as a language construct, should be more efficient than the resulting function call overhead when using array_key_exists().

As I highlighted in my previous example, the only way to test of an array has an index which is set to null is to use the array_key_exists() function as both isset() (also a language construct) and empty() will not be able to tell you this.

So your example above (which sets an array index to null) is the only way to do what you want to do (the switch() is overkill for an if/else scenario but that's beside the point).


All I was saying was that for an array index value that is set to a known true/false value, or not set at all, using !empty() to test for when the value is true is the most efficient way.

Consider the following two attached test cases to check for a true value, two indexes deep in the array. One uses two calls to array_key_exits and then a comparison, the other uses !empty(). They are functionally equivalent but the results speak for themselves:

[colin@jimmy 0.11]$ time php test1.php
Done: 10000000
13.76user 0.01system 0:14.08elapsed 97%CPU (0avgtext+0avgdata 0maxresident)k
0inputs+0outputs (0major+2805minor)pagefaults 0swaps
[colin@jimmy 0.11]$ time php test2.php
Done: 10000000
3.06user 0.03system 0:03.13elapsed 99%CPU (0avgtext+0avgdata 0maxresident)k
0inputs+0outputs (0major+2797minor)pagefaults 0swaps


And a repeat for random variations:

[colin@jimmy 0.11]$ time php test1.php
Done: 10000000
14.58user 0.05system 0:14.77elapsed 99%CPU (0avgtext+0avgdata 0maxresident)k
0inputs+0outputs (0major+2805minor)pagefaults 0swaps
[colin@jimmy 0.11]$ time php test2.php
Done: 10000000
3.01user 0.01system 0:03.06elapsed 98%CPU (0avgtext+0avgdata 0maxresident)k
0inputs+0outputs (0major+2797minor)pagefaults 0swaps


There's a benchmark for you (not sure if it's the one you wanted) which proves that the !empty() is ~4.5x faster.

 Use !empty() :p

Col


--

Colin Guthrie
gmane(at)colin.guthr.ie
http://colin.guthr.ie/

Day Job:
  Tribalogic Limited [http://www.tribalogic.net/]
Open Source:
  Mandriva Linux Contributor [http://www.mandriva.com/]
  PulseAudio Hacker [http://www.pulseaudio.org/]
  Trac Hacker [http://trac.edgewall.org/]

Attachment: test1.php
Description: application/php

Attachment: test2.php
Description: application/php

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux