Created
January 31, 2012 04:21
-
-
Save ludofleury/1708784 to your computer and use it in GitHub Desktop.
PHP get_class vs ReflectionObject vs ReflectionClass
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This was a quick & dirty benchmark (for my own needs) to compare the method to get a class name in PHP | |
-ReflectionObject->getName(); | |
-get_class(); | |
-ReflectionClass->getName(); | |
It was performed on my local machine : MacBookPro with PHP 5.3.6 | |
You will find the result below and the code used for the 3 tests. | |
ReflectionObject | |
Memory : 750040 | |
Memory peak : 778752 | |
Time : 0.0341 ~ 0.0337 | |
----------------------- | |
get_class | |
Memory : 750048 | |
Memory peak : 778760 | |
Time : 0.0340 ~ 0.0332 | |
----------------------- | |
ReflectionClass | |
Memory : 750032 | |
Memory peak : 778744 | |
Time : 0.0341 ~ 0.0342 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$object = new Mock\Document(); | |
$time_start = microtime(true); | |
for($i = 0; $i < 10000; $i++) { | |
get_class($object); | |
} | |
$time_end = microtime(true); | |
echo 'Memory : '.memory_get_usage()."\n"; | |
echo 'Memory peak : '.memory_get_peak_usage()."\n"; | |
$time = $time_end - $time_start; | |
echo 'Time : '.$time."\n"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$object = new Mock\Document(); | |
$time_start = microtime(true); | |
for($i = 0; $i < 10000; $i++) { | |
$reflectedObject = new \ReflectionClass('Mock\Document'); | |
$reflectedObject->getName(); | |
} | |
$time_end = microtime(true); | |
echo 'Memory : '.memory_get_usage()."\n"; | |
echo 'Memory peak : '.memory_get_peak_usage()."\n"; | |
$time = $time_end - $time_start; | |
echo 'Time : '.$time."\n"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$object = new Mock\Document(); | |
$time_start = microtime(true); | |
for($i = 0; $i < 10000; $i++) { | |
$reflectedObject = new \ReflectionObject($object); | |
$reflectedObject->getName(); | |
} | |
$time_end = microtime(true); | |
echo 'Memory : '.memory_get_usage()."\n"; | |
echo 'Memory peak : '.memory_get_peak_usage()."\n"; | |
$time = $time_end - $time_start; | |
echo 'Time : '.$time."\n"; |
With newer PHP versions \hrtime()
will probably offer more accurate measurements than \microtime()
.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Performance for newer PHP (7.2.3):
FYI: The
get_class
method misses to explode the unqualified class name from name with namespace. I added this:get_class
Memory: 384768
Memory peak: 419312
Time: 0.0024218559265137
ReflectionClass
Memory: 384664
Memory peak: 419224
Time: 0.0073721408843994
ReflectionObject
Memory: 384664
Memory peak: 419232
Time: 0.0038409233093262