Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

difference between executing curl_close() once or frequently?

Tags:

php

curl

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 garbage collector does all of this and sometimes there is no need to close DB connection or call the __destruct method to release resources.

Since, that moment I actually started to think about where do I need to call it then? At the moment I'm interested with that question since I writing a small library for curl and I'd like to understand when do I need to user curl_close() function.

Thank you all for discussion and explanation of it.

like image 440
Eugene Avatar asked Oct 03 '10 13:10

Eugene


1 Answers

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)

like image 141
T.Todua Avatar answered Sep 22 '22 08:09

T.Todua