Voting

: one plus two?
(Example: nine)

The Note You're Voting On

Anonymous
14 years ago
It's often faster to use a foreache and array_keys than array_unique:

<?php

$max
= 1000000;
$arr = range(1,$max,3);
$arr2 = range(1,$max,2);
$arr = array_merge($arr,$arr2);

$time = -microtime(true);
$res1 = array_unique($arr);
$time += microtime(true);
echo
"deduped to ".count($res1)." in ".$time;
// deduped to 666667 in 32.300781965256

$time = -microtime(true);
$res2 = array();
foreach(
$arr as $key=>$val) {
$res2[$val] = true;
}
$res2 = array_keys($res2);
$time += microtime(true);
echo
"<br />deduped to ".count($res2)." in ".$time;
// deduped to 666667 in 0.84372591972351

?>

<< Back to user notes page

To Top