Skip to content

Instantly share code, notes, and snippets.

@Leko
Last active October 4, 2016 14:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Leko/de4475dfe79bd8446e4b1a8949c96244 to your computer and use it in GitHub Desktop.
Save Leko/de4475dfe79bd8446e4b1a8949c96244 to your computer and use it in GitHub Desktop.
<?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