Skip to content

Instantly share code, notes, and snippets.

@kamermans
Created October 26, 2015 23:34
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save kamermans/04d3e557efca29bf2123 to your computer and use it in GitHub Desktop.
Save kamermans/04d3e557efca29bf2123 to your computer and use it in GitHub Desktop.
PHP time() vs microtime() vs microtime(true) benchmark
PHP
--------------------------------------------------------------
Total Iterations: 10,000,000
time(): 0.73976612091064 seconds (13,517,785/sec)
microtime(): 4.2245488166809 seconds (2,367,116/sec)
microtime(true): 1.2555038928986 seconds (7,964,929/sec)
HHVM
--------------------------------------------------------------
Total Iterations: 10,000,000
time(): 0.081642866134644 seconds (122,484,675/sec)
microtime(): 4.1530148983002 seconds (2,407,889/sec)
microtime(true): 0.37003302574158 seconds (27,024,614/sec)
<?php
$total = 10000000;
echo "Total Iterations: ".number_format($total)."\n";
echo "time(): ";
$start = microtime(true);
for ($i=0; $i<$total; $i++) {
$time = time();
}
$dur = microtime(true) - $start;
$perf = number_format(floor($total / $dur));
echo "$dur seconds ($perf/sec)\n";
echo "microtime(): ";
$start = microtime(true);
for ($i=0; $i<$total; $i++) {
$time = microtime();
}
$dur = microtime(true) - $start;
$perf = number_format(floor($total / $dur));
echo "$dur seconds ($perf/sec)\n";
echo "microtime(true): ";
$start = microtime(true);
for ($i=0; $i<$total; $i++) {
$time = microtime(true);
}
$dur = microtime(true) - $start;
$perf = number_format(floor($total / $dur));
echo "$dur seconds ($perf/sec)\n";
@jarbey
Copy link

jarbey commented Oct 21, 2018

Thx for bench !

@xpectmore
Copy link

xpectmore commented Feb 13, 2019

cool!
changing the experiment like this .. i obtain other results

<?php
$i=10000000;
echo("Total Iterations:".$i. "<br>\n");
$start = microtime(true);
while($i--) {
	$time = time();
}
$end1 = microtime(true);
$i=10000000;
while($i--) {
	$time = microtime();
}
$end2 = microtime(true);
$i=10000000;
while($i--) {
	$time = microtime(true);
}
$end3 = microtime(true);
$i=10000000;
while($i--) {
	$time = $_SERVER['REQUEST_TIME'];
}
$end4 = microtime(true);

echo 'time '.($end1 - $start) . "<br>\n";
echo 'microtime '.($end2 - $end1) . "<br>\n";
echo 'microtime true '.($end3 - $end2) . "<br>\n";
echo 'SERVER REQUEST_TIME '.($end4 - $end3) . "<br>\n";

echo 'Current PHP version: ' . phpversion();
?>

here i got:

Total Iterations:10000000
time 1.0563850402832
microtime 11.043941020966
microtime true 1.2809798717499
SERVER REQUEST_TIME 0.45594906806946
Current PHP version: 7.2.12

@MGazori
Copy link

MGazori commented Mar 11, 2022

Thank you!
here is my results in php 8.1.3

Round 1:
Total Iterations: 10,000,000
time(): 0.65436005592346 seconds (15,282,106/sec)
microtime(): 4.5953159332275 seconds (2,176,128/sec)
microtime(true): 0.67478799819946 seconds (14,819,469/sec)

Round 2
Total Iterations: 10,000,000
time(): 0.61929297447205 seconds (16,147,446/sec)
microtime(): 4.6143391132355 seconds (2,167,157/sec)
microtime(true): 0.6597728729248 seconds (15,156,731/sec)

Round 3
Total Iterations: 10,000,000
time(): 0.68782711029053 seconds (14,538,537/sec)
microtime(): 4.6785700321198 seconds (2,137,405/sec)
microtime(true): 0.73401689529419 seconds (13,623,664/sec)

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