Created
August 31, 2012 21:41
-
-
Save BaylorRae/3559529 to your computer and use it in GitHub Desktop.
Source code for: http://baylorrae.com/benchmarking-php-script-speeds/
This file contains hidden or 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 | |
class Benchmark { | |
/** | |
* Creates a loop that lasts for $allowed_time and logs how many | |
* times a function was able to run | |
* | |
* @param string $name the name of the test | |
* @param function $test the function to run | |
* @param integer $allowed_time seconds to run a function | |
* @return string | |
* @author Baylor Rae' | |
*/ | |
public static function run_test($name, $test, $allowed_time = 10) { | |
// get the time the function was called | |
$start_time = microtime(true); | |
// stores how many times $test was able to run | |
$times_run = 0; | |
// don't allow output | |
ob_start(); | |
// run the $test function until time is up | |
do { | |
call_user_func($test); | |
$times_run++; | |
}while( number_format(microtime(true) - $start_time, 0) < $allowed_time); | |
// end output buffering | |
ob_end_clean(); | |
// return the formatted results | |
return self::results($name, $times_run, $allowed_time); | |
} | |
/** | |
* Formats results for easy reading | |
* | |
* @param string $name name of the test | |
* @param integer $times_run number of times the test ran | |
* @param integer $allowed_time how long the test was allowed to run | |
* @return string | |
* @author Baylor Rae' | |
*/ | |
private static function results($name, $times_run, $allowed_time) { | |
$output = '<h2>Results for ' . $name . '</h2>'; | |
$output .= '<dl>'; | |
$output .= '<dt>Times Run</dt>'; | |
$output .= '<dd>' . number_format($times_run, 0) . '</dd>'; | |
$output .= '<dt>Ran For</dt>'; | |
$output .= '<dd>' . $allowed_time . 's</dd>'; | |
$output .= '</dl>'; | |
return $output; | |
} | |
} | |
?> |
This file contains hidden or 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 | |
require 'Benchmark.php'; | |
// a place to store the result of each test | |
$results = array(); | |
// my version of un-camel casing a word | |
function test_1() { | |
return strtolower(implode(' ', preg_split('/(?<=\\w)([A-Z])/', 'aCamelCasedWord'))); | |
} | |
// CakePHP's version of un-camel casing a word | |
function test_2() { | |
return strtolower(preg_replace('/(?<=\\w)([A-Z])/', '_\\1', 'aCamelCasedWord')); | |
} | |
/** | |
* @param title | |
* @param function/test | |
* @param allowed time to run | |
*/ | |
$results[] = Benchmark::run_test('My Version', 'test_1', 10); | |
$results[] = Benchmark::run_test('Cake\'s Version', 'test_2', 10); | |
foreach( $results as $result ) | |
echo $result; | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment