php 中的嵌套迭代有多慢?
在我的嵌套空迭代中,$arr1有90000个项目,当$arr2有9000个项目时,整个嵌套迭代使用了不到3分钟,
但是当$arr2只添加1000时到10000个项目,时间飞到超过100分钟,很好奇,与cpu、内存、IO或php有关本身?
$arr1 = array(...);
$arr2 = array(...);
$starttime = time();
foreach($arr1 as $v1){
foreach($arrs as $v2){
}
}
$endtime = time() - $starttime;
In my nested empty iteration, $arr1 has 90000 items, when $arr2 has 9000 items, the whole nested iteration used less then 3 minutes,
but when $arr2 just add 1000 to 10000 items, the time fly to more then 100 minutes, so curious, have it to do with cpu,memory, IO or php itself?
$arr1 = array(...);
$arr2 = array(...);
$starttime = time();
foreach($arr1 as $v1){
foreach($arrs as $v2){
}
}
$endtime = time() - $starttime;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我建议您阅读一些算法复杂性的书。 (在google books上查看这个主题)
100分钟的问题是你的算法设计,而不是php、cpu等(这当然也很重要)。
看来你的是 O(n^2) ,但是 for 循环内没有代码,我不能说它是否比这更糟糕。
希望有帮助
I recommend you to read some book of algorithmic complexity. (check this topic at google books)
The problem of 100 minutes is your algorithm design, not php, cpu ,etc (this also matters of course).
Seems yours is O(n^2) , but without code inside the for loops, I cant said if it is worse than that.
Hope it helps