- microtime: documentation ( source)
- array_merge: documentation ( source)
- array_flip: documentation ( source)
- ini_set: documentation ( source)
- array_keys: documentation ( source)
- array_unique: documentation ( source)
- range: documentation ( source)
<?php
ini_set('memory_limit', '512m');
$max = 100000;
$arr = range(1,$max,3);
$arr2 = range(1,$max,2);
$arr = array_merge($arr,$arr2);
//echo '<pre>'; print_r($arr); echo '</pre>';
// -----------------------------------------
$time = -microtime(true);
$res1 = array_unique($arr);
$time += microtime(true);
echo "deduped to ".count($res1)." in ".$time;
//echo '<pre>'; print_r($res1); echo '</pre>';
// deduped to 666667 in 32.300781965256
unset($res1);
$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;
//echo '<pre>'; print_r($res2); echo '</pre>';
// deduped to 666667 in 0.84372591972351
unset($res2);
// array_flip
$time = -microtime(true);
$res3 = array_flip(array_flip($arr));
//$res3 = array_flip($res3);
$time += microtime(true);
echo "<br />deduped to ".count($res3)." in ".$time;