试图了解array_diff_uassoc优化 [英] Trying to understand array_diff_uassoc optimization

查看:159
本文介绍了试图了解array_diff_uassoc优化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

似乎阵列比较对方内线 array_diff_uassoc

It seems that arrays sorted before comparing each other inside array_diff_uassoc.

什么是这种方法的好处?

What is the benefit of this approach?

测试脚本

function compare($a, $b)
    {
    echo("$a : $b\n");
    return strcmp($a, $b);
    }

$a = array('a' => 1, 'b' => 2, 'c' => 3, 'd' => 4, 'e' => 5);
$b = array('v' => 1, 'w' => 2, 'x' => 3, 'y' => 4, 'z' => 5);
var_dump(array_diff_uassoc($a, $b, 'compare'));


$a = array('a' => 1, 'b' => 2, 'c' => 3, 'd' => 4, 'e' => 5);
$b = array('d' => 1, 'e' => 2, 'f' => 3, 'g' => 4, 'h' => 5);
var_dump(array_diff_uassoc($a, $b, 'compare'));


$a = array('a' => 1, 'b' => 2, 'c' => 3, 'd' => 4, 'e' => 5);
$b = array('a' => 1, 'b' => 2, 'c' => 3, 'd' => 4, 'e' => 5);
var_dump(array_diff_uassoc($a, $b, 'compare'));

$a = array('a' => 1, 'b' => 2, 'c' => 3, 'd' => 4, 'e' => 5);
$b = array('e' => 5, 'd' => 4, 'c' => 3, 'b' => 2, 'a' => 1);
var_dump(array_diff_uassoc($a, $b, 'compare'));

http://3v4l.org/DKgms#v526

P.S。似乎排序算法中php7改变。

P.S. it seems that sorting algorithm changed in php7.

推荐答案

排序算法在PHP没有改变7.元素只是通过在另一个以排序算法的一些性能改进。

Sorting algorithm didn't change in PHP 7. Elements are just passed in another order to the sorting algorithm for some performance improvements.

好了,好处可能是最终执行速度更快。你真的遇到最坏的情况下,当两个数组有完全其它按键。

Well, benefit could be an eventual faster execution. You really hit worst case when both arrays have completely other keys.

最坏情况的复杂度的两倍排序阵列,然后将两个阵列的每个键的比较。 O(N * M + N *的log(n)+ M *日志(M))

Worst case complexity is twice sorting the arrays and then comparisons of each key of the two arrays. O(n*m + n * log(n) + m * log(m))

最好的情况是两次排序,然后同样多的比较,因为有较小的数组中的元素。 O(分钟(M,N)+ N *的log(n)+ M *日志(M))

Best case is twice sorting and then just as many comparisons as there are elements in the smaller array. O(min(m, n) + n * log(n) + m * log(m))

在匹配的情况下,你就不会再对比较完整的数组,但只从比赛后的关键。

In case of a match, you wouldn't have to compare against the full array again, but only from the key after the match on.

但在当前实现中,排序仅仅是多余的。在PHP-SRC实施需要一些改进,我认为。有没有彻底的错误,而是实现只不过是坏的。如果你了解一些C: http://lxr.php.net /xref/PHP_TRUNK/ext/standard/array.c#php_array_diff
(请注意,该功能是通过称为php_array_diff(INTERNAL_FUNCTION_PARAM_PASSTHRU,DIFF_ASSOC,DIFF_COMP_DATA_INTERNAL,DIFF_COMP_KEY_USER); array_diff_uassoc

But in current implementation, the sorting is just redundant. Implementation in php-src needs some improvement I think. There's no outright bug, but implementation is just bad. If you understand some C: http://lxr.php.net/xref/PHP_TRUNK/ext/standard/array.c#php_array_diff (Note that that function is called via php_array_diff(INTERNAL_FUNCTION_PARAM_PASSTHRU, DIFF_ASSOC, DIFF_COMP_DATA_INTERNAL, DIFF_COMP_KEY_USER); from array_diff_uassoc)

这篇关于试图了解array_diff_uassoc优化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆