CamaroSS
CamaroSS

Reputation: 493

PHP execution time measurement precision

So I measure execution time like this:

$time = -microtime(true);
...
$time += microtime(true);

PHP version is 5.3.26RC1 x86 running on Win Server 2008 x64 machine. "Precision" ini parameter equals 16. However, each time the execution time is below 0.001, it just returns zero (obviously, the script can't be executed instantly!). What's even more strange, my test machine (standard php.ini-development) returns nonzero values even if there is no code between these two statements... What could be wrong?

Upd: Simple test:

<?php
ini_set('precision',16);
echo php_uname() . PHP_EOL;
echo phpversion() . PHP_EOL;
echo ini_get('precision').PHP_EOL;

$time = -microtime(true);
$time += microtime(true);
echo number_format($time, 10) .PHP_EOL;
?>

Test machine output:

Windows NT XXX 6.1 build 7601 (Windows 7 Ultimate Edition Service Pack 1) i586
5.4.12
16
0.0000028610

Production machine output:

Windows NT XXX 6.1 build 7601 (Windows Server 2008 R2 Standard Edition Service Pack 1) i586
5.3.26RC1
16
0.0000000000

Upvotes: 2

Views: 341

Answers (1)

Prof. Falken
Prof. Falken

Reputation: 24937

Your platform, Windows, can not discriminate between such small times, with the standard time calls.

A comment from the PHP source code:

"on windows, time_sleep_until has millisecond accuracy while microtime() is accurate to 10th of a second."

(In ext/standard/tests/misc/time_sleep_until_basic.phpt.)

Upvotes: 5

Related Questions