Bug->Req #70754 [Opn]: array_udiff should compare the same values only once

From: Date: Fri, 06 Aug 2021 12:48:00 +0000
Subject: Bug->Req #70754 [Opn]: array_udiff should compare the same values only once
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-235646@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=70754&edit=1 ID: 70754 Updated by: cmb@php.net Reported by: php_bugs at multiwebinc dot com -Summary: array_udiff compares the same values multiple times +Summary: array_udiff should compare the same values only once Status: Open -Type: Bug +Type: Feature/Change Request Package: Performance problem PHP Version: 7.0.0RC5 Block user comment: N Private report: N New Comment: First, the details are a bit different as of PHP 7.0.0[1] (small arrays use insertion sort instead of quicksort), but generally, yes, there may be "unnecessary" comparisons. The point is, that for efficiency reasons, array_udiff() sorts all arrays first, and only afterwards calculates the actual diff. None of the comparison results are cached, so you suffer the additional function call overhead for the usually cheap comparisons (for rather expensive comparisons, you can cache the results yourself). So there may be room for improvement, but there is no bug per se. [1] <https://3v4l.org/LBeQB> Previous Comments: ------------------------------------------------------------------------ [2015-10-21 01:40:38] php_bugs at multiwebinc dot com 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. ------------------------------------------------------------------------ -- Edit this bug report at https://bugs.php.net/bug.php?id=70754&edit=1

« previous php.bugs (#235646) next »