Skip to content

Instantly share code, notes, and snippets.

@masakielastic
Created August 12, 2012 08:14
Show Gist options
  • Save masakielastic/3330620 to your computer and use it in GitHub Desktop.
Save masakielastic/3330620 to your computer and use it in GitHub Desktop.
Benchmark curl_setopt_array vs curl_setopt
<?php
function benchmark(callable $block) {
$start = microtime(true);
for ($i = 0; $i < 100000; ++$i) {
$block();
}
$end = microtime(true);
return $end - $start;
}
echo 'curl_setopt_array: ', benchmark(function() {
$options = array(
CURLOPT_RETURNTRANSFER => TRUE,
CURLOPT_HTTPHEADER => array('Expect:'),
CURLOPT_HEADER => FALSE
);
$ci = curl_init();
curl_setopt_array($ci, $options);
}), PHP_EOL;
echo ' curl_setopt: ', benchmark(function() {
$ci = curl_init();
curl_setopt($ci, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ci, CURLOPT_HTTPHEADER, array('Expect:'));
curl_setopt($ci, CURLOPT_HEADER, FALSE);
}), PHP_EOL;
curl_setopt_array: 1.2236759662628
curl_setopt: 1.384241104126
@BobbyWibowo
Copy link

My result on a Skylake CPU:
curl_setopt_array: 0.41492700576782
curl_setopt: 0.42061114311218
So I suppose even the speed is slightly noticeable in my machine.

@oantonioo
Copy link

in 2023 with PHP v8.2, it seems that the result has turned around.
although it is true that the times were reduced for the 2:

curl_setopt_array: 0.20178509712219
curl_setopt: 0.17806507110596

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment