Copy CodeThe code is as follows:
$arrF = Array ();
$arrS = Array ();
$intTotal = 100;
$intRand = 10;
for ($i =0; $i < $intTotal; $i + +)
{
$arrF [] = rand (1, $intRand);
$arrS [] = rand (1, $intRand);
}
$arrT = Array_merge ($arrF, $arrS);
$arrRF = Array ();
$intStart = time ();
foreach ($arrT as $v)
{
if (In_array ($v, $arrRF))
{
Continue
}
Else
{
$arrRF [] = $v;
}
}
$intEnd = time ();
$intTime = $intEnd-$intStart;
echo "With Continue,spend time: $intTime
";
$intStart 1 = time ();
$arrRS = Array_unique ($arrT);
$intEnd 2 = time ();
$intTime 2 = $intEnd 2-$intStart 1;
echo "With Array_unique function,spend Time: ($intTime 2)";
echo "
echo "
";
?>
In the case of small $inttotal, for example, within 1000, the value of $intRand basically does not affect the results, the two execution time is similar.
When the test $inttotal is greater than 10000, the efficiency of using array_unique is higher than that of the Foreach loop when the value of the $intRand is 100, $intRand = 10, and the execution time is consistent.
Therefore, it can be concluded that when the array size is not large, probably within 1000, the implementation efficiency is similar.
When the size of the array is large (what should be to what value, I have no detailed test, interested can determine this value), with the gradual increase of $intrand, array_unique performance better, I do not use $inttotal/$intRand this ratio, is because, The feeling is not proportional change, but the more basic will follow the ratio, array_unique performance better.
To sum up, in the filter array of duplicate values, it is recommended to use Array_unuique, when the array is not as efficient as the two, and array_unique use of course let your code suddenly reduced a few lines, the array capacity is too large, function performance better, why not?
http://www.bkjia.com/PHPjc/323965.html www.bkjia.com true http://www.bkjia.com/PHPjc/323965.html techarticle Copy the code as follows: PHP $arrF = Array (), $arrS = Array (), $intTotal = n, $intRand = ten, for ($i =0; $i $intTotal; $i + +) {$arr F[] = rand (1, $intRand); $arrS [] = rand (1, $in ...