note 93145 added to function.get-class
| From: | OlivierHubert-phpATraggartDOTnet at osu1 dot php dot net | Date: | Tue, 25 Aug 2009 15:43:52 +0000 |
| Subject: | note 93145 added to function.get-class | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-159873@lists.php.net to get a copy of this message | ||
For those of you who are wondering whether it is faster to use get_class or instanceof, I wrote the
following piece of code to test it:
<?php
$TestObject = new TestingObject;
$InstanceOfTimeBefore = microtime(true);
for($Counter = 0; $Counter < 10000000; $Counter++) $Result = $TestObject instanceof
TestingObject;
$InstanceOfTimeAfter = microtime(true);
$GetClassTimeBefore = microtime(true);
for($Counter = 0; $Counter < 10000000; $Counter++) $Result = get_class($TestObject);
$GetClassTimeAfter = microtime(true);
echo "Time for instanceof: " . number_format($InstanceOfTimeAfter -
$InstanceOfTimeBefore, 3) . "\n";
echo "Time for get_class: " . number_format($GetClassTimeAfter - $GetClassTimeBefore, 3)
. "\n";
?>
The results on my server:
Time for instanceof: 5.585
Time for get_class: 7.788
So instanceof is a bit faster. Keep in mind that I had to check 10 million times in order to get a
difference of that magnitude.
My conclusion is that it probably does not matter which one you use since both ways are extremely
fast.
----
Server IP: 66.207.199.35
Probable Submitter: 64.235.206.57
----
Manual Page -- http://www.php.net/manual/en/function.get-class.php
Edit -- https://master.php.net/note/edit/93145
Del: integrated -- https://master.php.net/note/delete/93145/integrated
Del: useless -- https://master.php.net/note/delete/93145/useless
Del: bad code -- https://master.php.net/note/delete/93145/bad+code
Del: spam -- https://master.php.net/note/delete/93145/spam
Del: non-english -- https://master.php.net/note/delete/93145/non-english
Del: in docs -- https://master.php.net/note/delete/93145/in+docs
Del: other reasons-- https://master.php.net/note/delete/93145
Reject -- https://master.php.net/note/reject/93145
Search -- https://master.php.net/manage/user-notes.php