Created
April 19, 2011 02:44
-
-
Save benmatselby/926715 to your computer and use it in GitHub Desktop.
Test to see which is quicker, explode or substring to find the last part of a php class name
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 | |
// How many times to we want to iterate | |
$count = isset($argv[1]) ? $argv[1] : 1000; | |
// Which function are we wanting to test | |
if (isset($argv[2]) && in_array($argv[2], array('explodeVersion', 'substringVersion'))) { | |
$function = $argv[2]; | |
} else { | |
$function = 'explodeVersion'; | |
} | |
// main | |
echo 'Functions to test:'.PHP_EOL; | |
echo ' - explodeVersion'.PHP_EOL.' - substringVersion'.PHP_EOL.PHP_EOL; | |
echo 'Started: '.date('d-m-Y H:i:s').PHP_EOL; | |
echo 'Running with function: '.$function.PHP_EOL; | |
$timerStart = microtime(true); | |
iterate($count, $function); | |
$timerEnd = microtime(true); | |
$overall = $timerEnd - $timerStart; | |
echo 'Time taken: '.sprintf('%0.6f', $overall).PHP_EOL; | |
echo 'Peak Memory Usage: '.memory_get_peak_usage().PHP_EOL; | |
echo 'Completed: '.date('d-m-Y H:i:s').PHP_EOL; | |
/** | |
* Iterate for data generation | |
* | |
* @param int $count How many times we want to iterate | |
* @param string $function Which function we want to run | |
* | |
* @return string | |
*/ | |
function iterate($count, $function) { | |
$class = 'DocBlox_Parser_DocBlock_Validator_File'; | |
for ($i = 0; $i <= $count; $i++) { | |
$part = $function($class); | |
echo "Iterating: $i\r"; | |
} | |
echo PHP_EOL; | |
} | |
/** | |
* Explode Version | |
* | |
* @param string $string The class name | |
* | |
* @return string | |
*/ | |
function explodeVersion($string) | |
{ | |
$parts = explode('_', $string); | |
return $parts[count($parts) - 1]; | |
} | |
/** | |
* Substring Version | |
* | |
* @param string $string The class name | |
* | |
* @return string | |
*/ | |
function substringVersion($string) | |
{ | |
return substr($string, strrpos($string, '_') + 1); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment