简体   繁体   English

如何在PHP循环中加快单个查询的速度?

[英]How to speed up single query in an loop in PHP?

I try to create an stock web app by php. 我尝试通过php创建一个股票Web应用程序。

There is an stock list (about 2,000 stocks), each stock with an specified ID. 有一个库存清单(约2,000只股票),每只股票都有指定的ID。

Then I set an loop to fetch the latest price from other site by function file_get_contents 然后,我设置一个循环以通过函数file_get_contents从其他站点获取最新价格

Here is the pseudo-code, 这是伪代码,

<?php
$stock_arr=Array();
//get stock ID list from my database then fill the $stock_arr array.
for($i=0;$i<count(stock_arr);$i++){

    $cur_id=stock_arr[$i];
    $url="http://example.com/r=".$cur_id;
    $json=file_get_contens($url);
    updateprice($json,$cur_id);

}

I had set an simple timer,and found each query spends about 0.3 seconds. 我设置了一个简单的计时器,发现每个查询花费大约0.3秒。

For 2,000 stocks,it`s about 6~8 mins. 对于2,000股,大约需要6到8分钟。 And I dont think it is acceptable. 而且我不认为这是可以接受的。

Since the query and update of each stock were not related. 由于每个库存的查询和更新都不相关。 I thought there might be better algorithm. 我认为可能会有更好的算法。

Could someone give me some hint to speed up the process?Thanks. 有人可以给我一些加快流程的提示吗?

搜索“ curl multi example php”,您可以在此处找到类似的功能http://kahthong.com/2012/04/php-parallel-curl-request-curlmultiexec-and-curlmultigetcontent

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM