Last active
August 29, 2015 14:16
-
-
Save nineinchnick/ab6a3ccc938eeec238f5 to your computer and use it in GitHub Desktop.
php benchmark
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 | |
// define benchmarked functions | |
$f1 = function ($string, $delimiter = ',', $trim = true, $skipEmpty = false) { | |
$result = []; | |
$parts = explode($delimiter, $string); | |
foreach ($parts as $value) { | |
if ($trim) { | |
$value = $trim === true ? trim($value) : (is_callable($trim) ? $trim($value) : trim($value, $trim)); | |
} | |
if ($skipEmpty && $value === '') { | |
continue; | |
} | |
$result[] = $value; | |
} | |
return $result; | |
}; | |
$f2 = function ($string, $delimiter = ',', $trim = true, $skipEmpty = false) { | |
$result = explode($delimiter, $string); | |
if ($trim) { | |
$result = array_map($trim === true ? 'trim' : (is_callable($trim) ? $trim : function($v)use($trim){return trim($v, $trim);}), $result); | |
} | |
if ($skipEmpty) { | |
$result = array_filter($result); | |
} | |
return $result; | |
}; | |
$f3 = function ($string, $delimiter = ',', $trim = true, $skipEmpty = false) { | |
if ($skipEmpty) { | |
if ($trim) { | |
return array_filter(array_map($trim === true ? 'trim' : (is_callable($trim) ? $trim : function($v)use($trim){return trim($v, $trim);}), explode($delimiter, $string))); | |
} | |
return array_filter(explode($delimiter, $string)); | |
} | |
if ($trim) { | |
return array_map($trim === true ? 'trim' : (is_callable($trim) ? $trim : function($v)use($trim){return trim($v, $trim);}), explode($delimiter, $string)); | |
} | |
return explode($delimiter, $string); | |
}; | |
// setup benchmark params | |
$iterations = 100000; | |
$arguments = array( | |
' trim empty' => array('abc,def,iijaef9,13498 ,aef 981341, 98afja ,,Здесь строка , , ,, ', ',', true, false), | |
'notrim empty' => array('abc,def,iijaef9,13498 ,aef 981341, 98afja ,,Здесь строка , , ,, ', ',', false, false), | |
' trim noempty' => array('abc,def,iijaef9,13498 ,aef 981341, 98afja ,,Здесь строка , , ,, ', ',', true, true), | |
'notrim noempty' => array('abc,def,iijaef9,13498 ,aef 981341, 98afja ,,Здесь строка , , ,, ', ',', false, true), | |
); | |
$functions = array( | |
'original' => $f1, | |
'array_*' => $f2, | |
'array_* w/o vars' => $f3, | |
); | |
$checkValues = false; | |
// run benchmark | |
$benchmark = function ($callable, $arguments, $iterations = 100000) { | |
$start_time = microtime(TRUE); | |
for($i = 0; $i < $iterations; $i++) { | |
$value = call_user_func_array($callable, $arguments); | |
} | |
$end_time = microtime(TRUE); | |
$total_time = $end_time - $start_time; | |
return array($value, $total_time); | |
}; | |
$results = array(); | |
$previous = null; | |
foreach($arguments as $arguments_name => $argument) { | |
foreach($functions as $function_name => $function) { | |
list($value, $time) = $benchmark($function, $argument); | |
if ($checkValues && $previous !== null && $previous !== $value) { | |
echo "error: $function_name ($arguments_name) returned different results:\n"; | |
var_dump($previous, $value); | |
} | |
$previous = $value; | |
$results[$function_name] = $time; | |
} | |
// print results | |
asort($results); | |
$min_time = reset($results); | |
echo "$arguments_name:\n"; | |
foreach($results as $name => $time) { | |
echo str_pad($name.": ", 25, ' ', STR_PAD_LEFT).$time.' ('.(number_format(100*$time/$min_time, 2)).'%)'."\n"; | |
} | |
} | |
echo "\nPerformed $iterations iterations.\n\n"; | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment