Yesterday, I showed my implentation of Don Knuth’s on-line standard deviation implementation.
Today, I have for you, my interested reader, the results of a quick benchmark on my code versus the PECL stats package.
Here’s the script for the benchmark:
function stddev($array){
//Don Knuth is the $deity of algorithms
//http://en.wikipedia.org/wiki/Algorithms_for_calculating_variance#III._On-line_algorithm
$n = 0;
$mean = 0;
$M2 = 0;
foreach($array as $x){
$n++;
$delta = $x - $mean;
$mean = $mean + $delta/$n;
$M2 = $M2 + $delta*($x - $mean);
}
$variance = $M2/$n;
return sqrt($variance);
}
function mt($precision=4){ return round(microtime(true),$precision); }
function echodiff($start,$end){ echo ($end - $start) . " μs to execute\n"; }
//first, let's prepare a set of arrays.
mt_srand();
//four arrays on different orders of magnitude
$array10 = array();
$array100 = array();
$array1000 = array();
$array10000 = array();
$array100000 = array();
$array1000000 = array();
for($i=0;$i<10;$i++) $array10[] = mt_rand(0, 100);
for($i=0;$i<100;$i++) $array100[] = mt_rand(0, 100);
for($i=0;$i<1000;$i++) $array1000[] = mt_rand(0, 100);
for($i=0;$i<10000;$i++) $array10000[] = mt_rand(0, 100);
for($i=0;$i<100000;$i++) $array100000[] = mt_rand(0, 100);
for($i=0;$i<1000000;$i++) $array1000000[] = mt_rand(0, 100);
$ars = array($array10, $array100, $array1000, $array10000, $array100000, $array1000000);
foreach($ars as $ar){
echo "Trial of " . count($ar) . " elements.\n";
$mine_start = mt();
$mine_out = stddev($ar);
$mine_end = mt();
echo "Mine: ";
echodiff($mine_start, $mine_end);
$php_start = mt();
$php_out = stats_standard_deviation($ar);
$php_end = mt();
echo "PHP/PECL: ";
echodiff($php_start, $php_end);
}
?>
These are the results on my machine, an Athlon X2 6000+ with the max memory size in php.ini set to 2048M.
[colin@rahab Source]$ php stddev.php
Trial of 10 elements.
Mine: 9.98973846436E-5 μs to execute
PHP/PECL: 0 μs to execute
Trial of 100 elements.
Mine: 0.000200033187866 μs to execute
PHP/PECL: 0 μs to execute
Trial of 1000 elements.
Mine: 0.0019998550415 μs to execute
PHP/PECL: 0.000200033187866 μs to execute
Trial of 10000 elements.
Mine: 0.0218000411987 μs to execute
PHP/PECL: 0.00450015068054 μs to execute
Trial of 100000 elements.
Mine: 0.216400146484 μs to execute
PHP/PECL: 0.042799949646 μs to execute
Trial of 1000000 elements.
Mine: 1.13669991493 μs to execute
PHP/PECL: 0.213500022888 μs to execute
Approximately .8 μs isn’t much, but it could be higher if the dataset it much higher and the calculation is repeated several hundred times. Regardless, it shows that you should probably take the time to simply install PECL stats
and use it instead of trying to roll your own.
No comments:
Post a Comment