difference between executing curl_close() once or frequently?

前端 未结 3 1622
别那么骄傲
别那么骄傲 2021-01-17 16:37

When is it necessary to close curl connection and release resources consumed by it?

Why do I ask this question, well quite simply because I was told, that PHP garbag

3条回答
  •  刺人心
    刺人心 (楼主)
    2021-01-17 16:59

    Results for 100 times curl_exec (fetching url with cache avoiding):

    Executing in every loop:

    for ($i = 0; $i < 100; ++$i) {
        $c = curl_init();
        curl_setopt($c, CURLOPT_URL, "http://www.google.com/?rand=" . rand());
        curl_exec($c);
        curl_close($c);
    }
    

    8.5 seconds

    Executing only once :

    $c = curl_init();
    for ($i = 0; $i < 100; ++$i) {
        curl_setopt($c, CURLOPT_URL, "http://www.google.com/?rand=" . rand());
        curl_exec($c);
    }
    curl_close($c);
    

    5.3 seconds


    Decision: get used to always use optimal code in your tasks. (source)

提交回复
热议问题