php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #40970 filemtime/stat slower on PHP5 vs PHP4
Submitted: 2007-04-01 00:16 UTC Modified: 2007-04-10 21:17 UTC
From: php at edwardk dot info Assigned:
Status: Not a bug Package: Performance problem
PHP Version: 5.2.1 OS: Windows 2003
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: php at edwardk dot info
New email:
PHP Version: OS:

 

 [2007-04-01 00:16 UTC] php at edwardk dot info
Description:
------------
filemtime and other related functions are slower on php5 vs php4

Using PHP 5.2.1 and PHP 4.4.6 on Athlon X2 3800+, Windows 2003
the speed difference is about 50-100x slower.



Reproduce code:
---------------
<?
header('Content-type: text/plain');
function microtime_float()
{
    list($usec, $sec) = explode(" ", microtime());
    return ((float)$usec + (float)$sec);
}

$time_start = microtime_float();
for ($i = 1; $i <= 1000; $i++) {
    $blah = stat('.');
}
$time_end = microtime_float();
$time = $time_end - $time_start;
echo 'Took '.round(($time*100),3).'ms';
?>

Expected result:
----------------
Speeds should be similar

Actual result:
--------------
On PHP 4.4.6, it took about 1.6ms
On PHP 5.2.1 it took about 130ms

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2007-04-01 14:44 UTC] fmk@php.net
This is only the case when stat() fails.

If you change stat('.'); to stat('<some file>'); you will se that PHP5 is faster than php4. Almost 2x.
 [2007-04-01 20:42 UTC] php at edwardk dot info
I have modified the code to use, $blah = stat('.htaccess');
This file does exist, it is 161 bytes on NTFS.

PHP 4.4.6: 1.641ms
PHP 5.1.2: 108.29ms

Normally, I would be using the commands on many small files (400ish) in the current folder to determine if any of them had changed. This was fast enough on PHP4, but on PHP5, the same code takes much longer.
 [2007-04-02 20:13 UTC] stas@php.net
Might be related to stat cache usage... Try to see if you see the difference stat'ing a set of files in random order, not the same file repeatedly.
 [2007-04-02 20:32 UTC] php at edwardk dot info
Here's the new code:

<?
header('Content-type: text/plain');
function microtime_float()
{
    list($usec, $sec) = explode(" ", microtime());
    return ((float)$usec + (float)$sec);
}

$time_start = microtime_float();
$c = 0;
foreach (glob("*.torrent") as $filename) {
    $blah = stat($filename);
    $c++;
}
$time_end = microtime_float();
$time = $time_end - $time_start;
echo 'Took '.round(($time*100),3).'ms for '.$c.' files';
?>
and the results:
PHP 4.4.6
Took 5.232ms for 481 files
PHP 5.2.1
Took 107.762ms for 481 files
 [2007-04-02 20:38 UTC] php at edwardk dot info
One thing I have noticed is that when the PHP5 version runs, Apache2's kernel CPU time shoots up (measured in (Process Explorer) while processing the request where as in PHP4, CPU use remains low.
 [2007-04-10 10:35 UTC] php at edwardk dot info
I have new information, the slowness is present only when safe mode is on.
(for 1000 iterations)
For PHP 4.4.6
-----
G:\>C:\php4\php.exe -n -d safe_mode=On filemtime.php
X-Powered-By: PHP/4.4.6
Content-type: text/plain

Took 7.451ms
G:\>C:\php4\php.exe -n -d safe_mode=Off filemtime.php
X-Powered-By: PHP/4.4.6
Content-type: text/plain

Took 0.152ms
G:\>



For PHP 5.2.1
-----
G:\>C:\php5\php-cgi.exe -n -d safe_mode=On filemtime.php
X-Powered-By: PHP/5.2.1
Content-type: text/plain

Took 12.172ms
G:\>C:\php5\php-cgi.exe -n -d safe_mode=Off filemtime.php
X-Powered-By: PHP/5.2.1
Content-type: text/plain

Took 0.108ms
G:\>


Additionally, I checked with FileMon, and the requests made are different for each version (Safe mode On).

PHP 4.4.6
6:03:02.751 AM	php.exe:2344	OPEN	G:\	SUCCESS	Options: Open Directory  Access: 00100001	
6:03:02.751 AM	php.exe:2344	DIRECTORY	G:\	SUCCESS	FileBothDirectoryInformation: filemtime.php	
6:03:02.751 AM	php.exe:2344	CLOSE	G:\	SUCCESS		

PHP 5.2.1
6:03:04.158 AM	php-cgi.exe:2216	QUERY INFORMATION	G:\filemtime.php	SUCCESS	Attributes: A	
6:03:04.158 AM	php-cgi.exe:2216	OPEN	G:\	SUCCESS	Options: Open Directory  Access: 00100001	
6:03:04.158 AM	php-cgi.exe:2216	DIRECTORY	G:\	SUCCESS	FileBothDirectoryInformation: filemtime.php	
6:03:04.158 AM	php-cgi.exe:2216	CLOSE	G:\	SUCCESS		



note the additional "QUERY INFORMATION" access.


I've tried disabling safe mode and though performance has improved, it is still slower than the PHP4 version on my script when run under an apache module. The stats are now:

~40ms safe mode on (PHP5)
~15ms safe mode off (PHP5)

~5.5ms safe mode on (PHP4)
~0.7ms safe mode off (PHP4)
 [2007-04-10 21:17 UTC] stas@php.net
I tried to run php 4.4.6 vs. php 5.2.1 and on both examples I had PHP 5 faster than PHP 4. Maybe you have some other setting interfering? Try testing with empty php.ini - it would use defaults for both. 
 
PHP Copyright © 2001-2020 The PHP Group
All rights reserved.
Last updated: Thu Feb 27 15:01:28 2020 UTC