performance comparison with the curl php file_get_contents

The article about the "php file_get_contents using curl alternative" , the subsequent comparison of the results posted file_get_contents and curl, and curl performance comparison here except with file_get_contents, further comprising a comparison of their properties, the following results look view before speaking : [caption id = "attachment_1835" align = "aligncenter" width = "620"] php file_get_contents performance comparison with the curl [/ caption] curl performance comparison with the PHP source file_get_contents follows: 1829.php
<?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";
?>
Test Access http://test.ttlsa.com/html/1829.php file_get_contents speed: 4.2404510975 seconds curl speed: 2.8205530643 seconds curl file_get_contents speed faster than about 30%, most importantly, lower server load reprint please specify. source: PHP and curl under file_get_contents performance comparison http://www.ttlsa.com/html/1829.html  

Reproduced in: https: //my.oschina.net/766/blog/211357

Guess you like

Origin blog.csdn.net/weixin_34357436/article/details/91547241