[ create a new paste ] login | about

Link: http://codepad.org/diDhHJNo    [ raw code | output | fork ]

PHP, pasted on Jan 28:
<?php
function unique1($a) { return array_keys(array_flip($a)); }
function unique2($a) { return array_flip(array_flip($a)); }
function unique3($a) { return array_keys(array_count_values($a)); }

$test=array();
for($run=0; $run<1000; $run++)
$test[]=rand(0,100);

$time=microtime(true);

for($run=0; $run<100; $run++)
$out=array_keys(array_count_values($test));

$time=microtime(true)-$time;
echo 'Keys Count Values: '.$time."\n";

$time=microtime(true);

for($run=0; $run<100; $run++)
$out=unique3($test);

$time=microtime(true)-$time;
echo 'Keys Count Values in function wrapper: '.$time."\n";


$time=microtime(true);

for($run=0; $run<100; $run++)
$out=array_unique($test);

$time=microtime(true)-$time;
echo 'Array Unique: '.$time."\n";

$time=microtime(true);

for($run=0; $run<100; $run++)
$out=unique1($test);

$time=microtime(true)-$time;
echo 'Keys Flip in function wrapper: '.$time."\n";

$time=microtime(true);

for($run=0; $run<100; $run++)
$out=array_keys(array_flip($test));

$time=microtime(true)-$time;
echo 'Keys Flip: '.$time."\n";

$time=microtime(true);

for($run=0; $run<100; $run++)
$out=unique2($test);

$time=microtime(true)-$time;
echo 'Flip Flip in function wrapper: '.$time."\n";

$time=microtime(true);

for($run=0; $run<100; $run++)
$out=array_flip(array_flip($test));

$time=microtime(true)-$time;
echo 'Flip Flip: '.$time."\n";

?>


Output:
1
2
3
4
5
6
7
Keys Count Values: 0.0065281391143799
Keys Count Values in function wrapper: 0.0062530040740967
Array Unique: 1.4264948368073
Keys Flip in function wrapper: 0.011411905288696
Keys Flip: 0.034986972808838
Flip Flip in function wrapper: 0.01154899597168
Flip Flip: 0.035277843475342


Create a new paste based on this one


Comments: