Mantis - Quercus
Viewing Issue Advanced Details
2186 minor always 11-18-07 15:06 11-19-07 20:16
closed 3.1.4  
none 3.1.4  
0002186: microtime() is only millisecond-accurate
I was doing some performance tests in compiled mode and noticed my run times had much less noise than expected:

$start_time = microtime(true);
include_once $_SERVER['PHP_ROOT'].'/lib/core/init.php';
print "elapsed = " . (microtime(true) - $start_time) . " sec\n";

elapsed = 0.006000041961669922 sec
elapsed = 0.003000020980834961 sec
elapsed = 0.0029997825622558594 sec
elapsed = 0.003000020980834961 sec
elapsed = 0.003000020980834961 sec
elapsed = 0.003000020980834961 sec
elapsed = 0.003000020980834961 sec
elapsed = 0.003000020980834961 sec
elapsed = 0.002000093460083008 sec
elapsed = 0.003000020980834961 sec

The Zend PHP interpreter is much more accurate. This lack of precision makes it much harder to do meaningful internal performance monitoring in a PHP app -- and in particular, to benchmark Quercus versus vanilla PHP and identify their respective bottlenecks -- since most operations will appear to take either no time at all or 1 millisecond depending on whether they happen to straddle a millisecond boundary.
 patch.txt [^] (1,509 bytes) 11-18-07 15:41

11-18-07 15:37   
Simpler test case:

$a = microtime(true);
for ($i = 0; $i < 10; $i++) ;
$b = microtime(true);
print ($b - $a) . "\n";

You'll get "0.0" from Quercus nearly every time you run this, and various small numbers from vanilla PHP.
11-18-07 15:45   
One possible fix attached; on Linux, at least, System.nanoTime() actually returns the nanosecond-accurate system time. Since that's not guaranteed by the JDK spec I test to see if it's true, and if not, fall back to the old behavior.

A more robust implementation would probably try to figure out the offset between nanoTime() and currentTimeMillis()*1000000, but this is at least better than the current situation.
11-19-07 20:16   

System.nanoTime() returns the system time for Linux machines
System.nanoTime() returns some arbitrary timer time for Windows machines

microtime() changed to return System.nanoTime() for non-windows machines, and System.currentTimeMillis() for windows