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"; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
With newer PHP versions
\hrtime()
will probably offer more accurate measurements than\microtime()
.