Considering the target was to use recursive function, I gave him the following code:
function removeDuplicates(&$array) {
static $newArray=array();
if(current($array)){
if(@++$newArray[current($array)] > 1) {
unset($array[key($array)]);
} else {
next($array);
}
removeDuplicates($array);
}
}
But still, this is not a very good approach. So i made few other functions and test them.
Using foreach:
function removeDuplicatesForeach(&$array) {A better approach, slightly faster then the recursive function, but still not fast enough. On a random generated array of 100 values, it still lasted 0.07 millis.
$newArray=array();
foreach($array as $key=>$val){
if(isset($newArray[$val])){
unset($array[$key]);
continue;
}
$newArray[$val] = 1;
}
}
So, i tried using array internal pointers:
function removeDuplicatesWhile(&$array){And the resutl was 0.12 ms. Not faster than foreach, and this will not work properly if you have a boolean false value.
$newArray=array();
do{
if(isset($newArray[current($array)])){
unset($array[key($array)]);
continue;
}
$newArray[current($array)] = 1;
} while(next($array) !== false);
}
Then i tried using php functions. Even if there are no php functions to remove duplicates, you can try using array_flip() and array_keys() functions.
function removeDuplicatesNatural(&$array) {The result: 0.1 ms. Still NOT faster then foreach. I think this is because the value given for array_flip is copied, not taken by reference.
$array = array_keys(array_flip($array));
}
This is just something I do when I get bored. So using foreach is not really faster then array_flip ... this depends on the array you are trying to filter.
I hope this will help someone ...
No comments:
Post a Comment