Last active
October 4, 2016 14:23
-
-
Save Leko/de4475dfe79bd8446e4b1a8949c96244 to your computer and use it in GitHub Desktop.
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 | |
function bench($name, $fn, array $args = array(), $step = 10000) { | |
$times = array(); | |
while($step--) { | |
$s = microtime(true); | |
call_user_func_array($fn, $args); | |
$e = microtime(true); | |
$times[] = $e - $s; | |
} | |
$total = array_sum($times); | |
$average = $total / count($times); | |
$max = max($times); | |
$min = min($times); | |
echo sprintf("%s\t%.4f\t%.4f\t%.4f\t%.4f", $name, $total, $average, $max, $min).PHP_EOL; | |
} | |
function call_with_cache($fn) | |
{ | |
$fn(__FILE__); | |
} | |
function call_nocache($fn) | |
{ | |
clearstatcache(); | |
$fn(__FILE__); | |
} | |
$stat_affected_functions = array( | |
'stat', | |
'lstat', | |
'file_exists', | |
'is_writable', | |
'is_readable', | |
'is_executable', | |
'is_file', | |
'is_dir', | |
'is_link', | |
'filectime', | |
'fileatime', | |
'filemtime', | |
'fileinode', | |
'filegroup', | |
'fileowner', | |
'filesize', | |
'filetype', | |
'fileperms' | |
); | |
echo "name\ttotal\tavg\tmax\tmin".PHP_EOL; | |
foreach ($stat_affected_functions as $fn) { | |
bench("{$fn}+cache", 'call_with_cache', array($fn)); | |
bench($fn, 'call_nocache', array($fn)); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment