比较大量值的有效方法 php
你好 我要比较大量的值,我使用了数组,但内存不足。数组中的值约为 5000000,对于每个值,将再次执行 5000000 的循环。简而言之,将执行 5000000 x 5000000 个周期。
我所做的只是运行两个循环。请让我知道一些有效的方法来执行此操作,因为该程序因内存而停止。
for($k=0;$k<sizeof($pid);$k++) // size of $pid = 5000000
{
$out =0;
for ($m=0;$m<sizeof($outid);$m++) // size of $out 5000000
{
if ($pid[$k] == $out[$m])
{
$out ++;
}
}
}
hi
i am to compare very large number of values, i used arrays, but run out of memory. the values in arrays are around 5000000, and for every values again a loop of 5000000 will execute. in short 5000000 x 5000000 cycles will be executed.
what i am doing is simply running two loops. please let me know some efficient way to do this as this program stops because of the memory.
for($k=0;$k<sizeof($pid);$k++) // size of $pid = 5000000
{
$out =0;
for ($m=0;$m<sizeof($outid);$m++) // size of $out 5000000
{
if ($pid[$k] == $out[$m])
{
$out ++;
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您可以对两个列表进行排序,则只需查看每个列表一次,因为您可以拥有第一个列表的索引和第二个列表的索引。如果第一个索引处的元素小于第二个索引处的元素,则增加第一个索引,否则增加第二个索引。然后,当您传递它们时,您只需跟踪有多少个元素是相等的。
If you could sort both lists you would only have to look at all of each list once because you could have an index for the first list and an index for the second list. If the element at the first index is less then the element at the second index, increase the first index, otherwise increase the second index. Then you just keep track of how many elements are equal as you pass over them.
对于大多数 VB 和 PHP 程序员来说,算法复杂性可能是一个复杂的主题 - 它并不是微不足道的。
方法 1
假设您找到了一种使用
O(n^2)
方法的方法,并假设您的计算机可以在 1 秒内执行 1000000 次比较,并且您现在开始循环2:东部时间下午 04:45 | 2010 年 6 月 23 日星期三
,那么循环将在 EET | 上午 6:58:05 结束2012 年 1 月 23 日星期一。我不是 PHP 专家,但我确信页面在必须提供服务的情况下有时间限制,否则会引发页面超时异常。该限制可以是 30 秒、90 秒或您定义的任何时间,但此循环所需的时间限制很愚蠢。
方法 2
您决定对数组进行排序,此操作对两个数组都需要
O(n log n)
时间,并且需要O(n log n)
进行比较。这将使总时间约为3 * O(n log n)
,即100.5 秒
。或者33.49 秒
(如果数组已预先排序)。如果我是您,我会选择方法 2。
如果您不确定如何对数组进行排序,请提出一个新问题并描述您的数据。简而言之,您需要一个用于数据实例的自定义比较器。要以
O(n log n)
进行比较,您不能使用线性比较,但需要一个高效的深度比较函数,通常内置于语言默认库中。如果您找不到它或不知道如何使用它,请提出另一个问题。Algorithm complexity can be a complex subject for most of VB and PHP programmers - it is not trivial.
Approach 1
Lets say you find a way to use your
O(n^2)
approach, and lets say your computer can execute 1000000 comparisons in 1 second, and you start the loop now2:04:45 pm EEST | Wednesday, June 23, 2010
, then the loop would end in6:58:05 am EET | Monday, January 23, 2012
.I'm not an expert in PHP, but I'm certain that page has a time limit under what it must be served or page timeout exception is throw. That limit can be 30s, 90s or what ever you define it, but your required timelimit for this loop is silly.
Approach 2
You decide to sort the array, this action takes
O(n log n)
for both arrays, andO(n log n)
to compare. This will make the total time to be around3 * O(n log n)
, that's just100.5 seconds
. Or33.49 seconds
, if arrays are presorted.I would pick the Approach 2, if I was you.
If you are uncertain, how to sort array, then ask a new question and describe your data. In short, you need a custom comparator for your data instances. To compare in
O(n log n)
you can not use linear compare, but need a efficient deep compare function, usually built in language default libraries. If you can not find it or do not know how to use it, then ask another question.你可以尝试 array-intersect 函数 http://ua.php.net/ Manual/en/function.array-intersect.php,它是基于 C 的,应该更加优化
you can try array-intersect function http://ua.php.net/manual/en/function.array-intersect.php, it's C based and should work more optimized