note 115887 added to function.method-exists
| From: | charlestonolaesatgmailnospaces | Date: | Fri, 10 Oct 2014 04:31:47 +0000 |
| Subject: | note 115887 added to function.method-exists | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-200614@lists.php.net to get a copy of this message | ||
I was wondering if caching the the methods in an array would have a faster lookup. So I ran a very
simple benchmark using xdebug_time_index() with 10000 iterations on each statement.
using PHP 5.3.13 btw
<?php
// using an actual instance of an object shows 0.10398316383362 secs
method_exist($object, $method);
?>
<?php
// using a string shows 0.12779307365417 secs
method_exist('ClassName', $method);
?>
<?php
$array = array(/*method names with numeric index*/);
// shows 0.10288906097412 secs
in_array($method, $array);
?>
<?php
$assoc = array( /*method name as key and value*/ );
// shows 0.017536878585815 secs
isset( $assoc[$method] );
?>
From the looks of the results, there is very little difference in method_exist and in_array. Isset
seems to the fastest and using a string as the first parameter is the slowest.
Please note that the test was done on multiple methods, not just one, the code presented above is to
show the results, not the actual test code that ran. Also, this was tested just out of curiosity and
I didn't set up a specific environment or used any profiling tools, and it was not meant to be
an official benchmark in anyway.
----
Server IP: 72.52.91.14
Probable Submitter: 108.181.200.19
----
Manual Page -- http://php.net/manual/en/function.method-exists.php
Edit -- https://master.php.net/note/edit/115887
Del: integrated -- https://master.php.net/note/delete/115887/integrated
Del: useless -- https://master.php.net/note/delete/115887/useless
Del: bad code -- https://master.php.net/note/delete/115887/bad+code
Del: spam -- https://master.php.net/note/delete/115887/spam
Del: non-english -- https://master.php.net/note/delete/115887/non-english
Del: in docs -- https://master.php.net/note/delete/115887/in+docs
Del: other reasons-- https://master.php.net/note/delete/115887
Reject -- https://master.php.net/note/reject/115887
Search -- https://master.php.net/manage/user-notes.php