脚本宝典收集整理的这篇文章主要介绍了php – 我应该关闭cURL吗?,脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
如果您不需要同步请求,请考虑使用curl_multi_ *函数(例如curl_multi_init,@L_404_2@等),这也提供了很大的性能提升.
更新:
我尝试使用一个新的句柄为每个请求和使用相同的句柄与以下代码:
ob_start(); //Trying to avoid setting as many curl options as possible $start_time = microtime(true); for ($i = 0; $i < 100; ++$i) { $rand = rand(); $ch = curl_init(); curl_setopt($ch,CURLOPT_URL,"http://www.GOOGLE@R_406_1718@/?rand=" . $rand); curl_exec($ch); curl_close($ch); } $end_time = microtime(true); ob_end_clean(); echo 'Curl wIThout handle reuse: ' . ($end_time - $start_time) . '<br>'; ob_start(); //Trying to avoid setting as many curl options as possible $start_time = microtime(true); $ch = curl_init(); for ($i = 0; $i < 100; ++$i) { $rand = rand(); curl_setopt($ch,"http://www.google.com/?rand=" . $rand); curl_exec($ch); } curl_close($ch); $end_time = microtime(true); ob_end_clean(); echo 'Curl with handle reuse: ' . ($end_time - $start_time) . '<br>';
并得到以下结果:
Curl without handle reuse: 8.5690529346466 Curl with handle reuse: 5.3703031539917
因此,重复使用相同的句柄时,实际上可以在多次连接到同一台服务器时提供显着的性能提升.我尝试连接到不同的服务器:
$url_arr = array( 'http://www.google.com/','http://www.bing.com/','http://www.yahoo.com/','http://www.slashdot.org/','http://www.stackoverflow.com/','http://github.com/','http://www.harVARd.edu/','http://www.gamefaqs.com/','http://www.mangaupdates.com/','http://www.cnn.com/' ); ob_start(); //Trying to avoid setting as many curl options as possible $start_time = microtime(true); foreach ($url_arr as $url) { $ch = curl_init(); curl_setopt($ch,$url); curl_exec($ch); curl_close($ch); } $end_time = microtime(true); ob_end_clean(); echo 'Curl without handle reuse: ' . ($end_time - $start_time) . '<br>'; ob_start(); //Trying to avoid setting as many curl options as possible $start_time = microtime(true); $ch = curl_init(); foreach ($url_arr as $url) { curl_setopt($ch,$url); curl_exec($ch); } curl_close($ch); $end_time = microtime(true); ob_end_clean(); echo 'Curl with handle reuse: ' . ($end_time - $start_time) . '<br>';
并得到以下结果:
Curl without handle reuse: 3.7672290802002 Curl with handle reuse: 3.0146431922913
表现仍然相当显着.
以上是脚本宝典为你收集整理的php – 我应该关闭cURL吗?全部内容,希望文章能够帮你解决php – 我应该关闭cURL吗?所遇到的问题。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。