|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
PatchesCPVsri (last revision 2015-10-21 09:48 UTC by mandersonaugusto at bol dot com dot br)Pull RequestsHistoryAllCommentsChangesGit/SVN commits
[2021-08-06 12:48 UTC] cmb@php.net
-Summary: array_udiff compares the same values multiple times
+Summary: array_udiff should compare the same values only once
-Type: Bug
+Type: Feature/Change Request
[2021-08-06 12:48 UTC] cmb@php.net
|
|||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Mon Oct 27 18:00:01 2025 UTC |
Description: ------------ All versions of PHP seem to be inefficient at executing array_udiff. The values in $a in the callback function are all compared twice. If the callback function is expensive, this would affect performance. Also, some comparisons are unnecessary. Test script: --------------- /* Note: The callback function just returns 0 every time, which means that all values in both arrays are considered equal. */ function callback($a, $b) { echo "\$a = $a; \$b= $b; "; echo "<br>\n"; return 0; } $array1 = array("first_0", "first_1", "first_2", "first_3", "first_4" ); $array2 = array("second_0", "second_1", "second_2", "second_3", "second_4"); $result = array_udiff($array1, $array2, "callback"); Actual result: -------------- $a = first_2; $b= first_1; $a = first_4; $b= first_2; $a = first_2; $b= first_0; $a = first_3; $b= first_2; $a = first_4; $b= first_3; // Unnecessary since we already know these are equal $a = first_1; $b= first_0; // Unnecessary since we already know these are equal $a = second_2; $b= second_1; $a = second_4; $b= second_2; $a = second_2; $b= second_0; $a = second_3; $b= second_2; $a = second_4; $b= second_3; // Unnecessary since we already know these are equal $a = second_1; $b= second_0; // Unnecessary since we already know these are equal $a = first_4; $b= second_4; $a = first_4; $b= first_3; // Duplicate. Already tested above. $a = first_3; $b= first_2; // Duplicate. Already tested above. $a = first_2; $b= first_1; // Duplicate. Already tested above. $a = first_1; $b= first_0; // Duplicate. Already tested above.