php下curl与file_get_contents性能对比

凉白开 PHP616,1192字数 1933阅读6分26秒阅读模式

上一篇讲了《php使用curl替代file_get_contents》, 后续贴出了curl和file_get_contents的对比结果,这边除了curl与file_get_contents的性能对比,还包含了他们的性能对比,讲之前看下如下的结果图:

file_get_contents

php下curl与file_get_contents性能对比

curl与file_get_contents性能对比PHP源代码如下:
1829.php文章源自运维生存时间-https://www.ttlsa.com/php/php-curl-file_get_contents/

<?php
/**
 * 通过淘宝IP接口获取IP地理位置
 * @param string $ip
 * @return: string
 **/
function getCityCurl($ip)
{
    $url="http://ip.taobao.com/service/getIpInfo.php?ip=".$ip;
    $ch = curl_init();
    $timeout = 5;
    curl_setopt ($ch, CURLOPT_URL, $url);
    curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
    $file_contents = curl_exec($ch);
    curl_close($ch);

    $ipinfo=json_decode($file_contents);
    if($ipinfo->code=='1'){
        return false;
    }
    $city = $ipinfo->data->region.$ipinfo->data->city;
    return $city;
}

function getCity($ip)
{
    $url="http://ip.taobao.com/service/getIpInfo.php?ip=".$ip;
    $ipinfo=json_decode(file_get_contents($url));
    if($ipinfo->code=='1'){
        return false;
    }
    $city = $ipinfo->data->region.$ipinfo->data->city;
    return $city;
}

// for file_get_contents
$startTime=explode(' ',microtime());
$startTime=$startTime[0] + $startTime[1];
for($i=1;$i<=10;$i++)
{
   echo getCity("121.207.247.202")."</br>";
}
$endTime = explode(' ',microtime());
$endTime = $endTime[0] + $endTime[1];
$totalTime = $endTime - $startTime;
echo 'file_get_contents:'.number_format($totalTime, 10, '.', "")." seconds</br>";

//for curl
$startTime2=explode(' ',microtime());
$startTime2=$startTime2[0] + $startTime2[1];
for($i=1;$i<=10;$i++)
{
   echo getCityCurl('121.207.247.202')."</br>";
}
$endTime2 = explode(' ',microtime());
$endTime2=$endTime2[0] + $endTime2[1];
$totalTime2 = $endTime2 - $startTime2;
echo "curl:".number_format($totalTime2, 10, '.', "")." seconds";
?>

测试访问
http://test.ttlsa.com/html/1829.php
file_get_contents速度:4.2404510975 seconds
curl速度:2.8205530643 seconds文章源自运维生存时间-https://www.ttlsa.com/php/php-curl-file_get_contents/

curl比file_get_contents速度快了30%左右,最重要的是服务器负载更低.文章源自运维生存时间-https://www.ttlsa.com/php/php-curl-file_get_contents/

转载请注明出处:php下curl与file_get_contents性能对比 https://www.ttlsa.com/html/1829.html文章源自运维生存时间-https://www.ttlsa.com/php/php-curl-file_get_contents/

 文章源自运维生存时间-https://www.ttlsa.com/php/php-curl-file_get_contents/

文章源自运维生存时间-https://www.ttlsa.com/php/php-curl-file_get_contents/文章源自运维生存时间-https://www.ttlsa.com/php/php-curl-file_get_contents/
weinxin
我的微信
微信公众号
扫一扫关注运维生存时间公众号,获取最新技术文章~
凉白开
  • 本文由 发表于 28/07/2013 13:32:56
  • 转载请务必保留本文链接:https://www.ttlsa.com/php/php-curl-file_get_contents/
  • file_get_contents
  • php curl
评论  6  访客  6
    • Justin
      Justin 0

      不错 来看看

      • 小李
        小李 9

        碍事发v根本就那么快

        • 番号姐115
          番号姐115 2

          wordpress里貌似使用了蛮多的file_get_contents()这个函数, 一个一个去修改wordpress代码吗?

            • 运维生存时间
              运维生存时间 7

              @ 番号姐115 如果是file_get_contents 本地文件,那是没有性能影响的,如果是远程http,那就要修改了,wordpress本生没有的,至少我没见过。估计是你安装的插件有这种做法。ttlsa有安装部分插件启用之后,后台非常缓慢,也就是这个问题造成的。

                • 番号姐115
                  番号姐115 2

                  @ 运维生存时间 插件的确会导致这种问题,想尽各种办法把插件修改为代码版本,修改的面目全非了

              • 哈哈哈
                哈哈哈 9

                支持评论功能更吗

              评论已关闭!