Implement code to filter out duplicate values in PHP arrays _php tutorial

Source: Internet
Author: User
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 ...

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.