php array_intersect()效率 [英] php array_intersect() efficiency

查看:237
本文介绍了php array_intersect()效率的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请考虑以下脚本.两个只有三个值的数组.当我使用array_intersect()比较这两个数组时.结果很快.

consider the below script. two arrays with only three values.when i compare these two arrays using array_intersect(). the result is fast.

    <?php
$arrayOne = array('3', '4', '5');
$arrayTwo = array('4', '5', '6');

$intersect = array_intersect($arrayOne, $arrayTwo);

print_r($intersect );

?>

我的问题是array_intersect()的效率是多少.是否比较两个均具有1000个值的数组.会产生更好的结果..... r我们需要使用一些哈希函数来快速查找公用值,这将是有效的......为此我需要您的建议...

my question is what is the efficiency of the array_intersect(). whether if we compare two arrays both having 1000 values each. would produce better result..... r we need to use some hash function to deal with finding common values quickly which will be effective???.. i need ur suggestion for this...

我正在做一个应用程序.如果有人来使用Facebook登录名登录,那么该应用程序将获取他的朋友列表,并查找之前在我的应用程序中是否有评论的朋友并显示给他.大约一个朋友在Facebook中可能有200到300个朋友,而db拥有1000多个记录.我需要找到有效的方法,我该怎么做....

i am doing an application.if an person comes and login using facebook login.then the application will get his friends list and find whether any friends as commented in my app before and show it to him. roughly a friends may have 200 to300 friends in facebook and db has more than 1000 records. i need to find that efficiently how can i do that.......

推荐答案

交集可以通过在第二个数组中构造一组搜索值来实现,并且在集合中查找可以如此之快以至于它基本上需要常量平均时间.因此,整个算法的运行时间可以在O(n)中.

Intersection can be implemented by constructing a set of the searched values in the second array, and looking up in a set can be made so fast that it takes essentially constant time on average. Therefore, the runtime of the whole algorithm can be in O(n).

或者,可以对第二个数组(在O(n log n)中)进行排序.由于在排序数组中查找在O(log n)中具有运行时,因此整个算法应在O(n log n)中具有运行时.

Alternatively, one can sort the second array (in O(n log n)). Since looking up in a sorted array has a runtime in O(log n), the whole algorithm should then have a runtime in O(n log n).

根据我刚进行的(简短,不科学的)测试,php的array_intersect似乎是这种情况:

According to a (short, unscientific) test I just ran, this seems to be the case for php's array_intersect:

这是我用来测试的代码.如您所见,对于小至1000的输入大小,您不必担心.

Here's the code I used to test it. As you can see, for an input size as small as 1000, you don't need to worry.

这篇关于php array_intersect()效率的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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