On 18-03-2010 23:15, Peter Farsinsen wrote:
> Martin wrote:
>
>>>
http://dk.php.net/manual/en/function.array-intersect.php
>> Dog skal det lige siges at det faktisk er hurtigere at lave det selv med
>> for løkker :)
>
> Har du benchmarket det? ;)
>
array_intersect timed at: 1.0257320404053 seconds
my_array_intersect timed at: 0.29034399986267 seconds
<?php
function remove_random($array, $num = 100) {
for($i = 0; $i < $num; $i++) {
unset($array[rand(0,1000)]);
}
return $array;
}
function my_array_intersect($a,$b)
{
for($i=0;$i<sizeof($a);$i++) $m[]=$a[$i];
for($i=0;$i<sizeof($a);$i++) $m[]=$b[$i];
sort($m);
$get=array();
for($i=0;$i<sizeof($m);$i++) {
if($m[$i]==$m[$i+1]) $get[]=$m[$i];
}
return $get;
}
$array1 = range(0,1000);
$array2 = remove_random(range(0,1000));
$times = 100;
$starttime = microtime(true);
for($i = 0; $i < $times; $i++) {
$result = array_intersect($array1, $array2);
}
echo 'array_intersect timed at: ' . (microtime(true) - $starttime) . '
seconds';
$starttime = microtime(true);
for($i = 0; $i < $times; $i++) {
$result = my_array_intersect($array1, $array2);
}
echo '<br />';
echo 'my_array_intersect timed at: ' . (microtime(true) - $starttime) .
' seconds';