note 93145 deleted from function.get-class by danbrown
| From: | danbrown@php.net | Date: | Tue, 25 Aug 2009 16:21:50 +0000 |
| Subject: | note 93145 deleted from function.get-class by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-159874@lists.php.net to get a copy of this message | ||
Note Submitter: Olivier Hubert - php AT raggart DOT net
----
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.