From: peoned at yahoo dot com
Operating system: Linux
PHP version: 5.0.4
PHP Bug Type: Performance problem
Bug description: fgets() function is slow
Description:
------------
fgets() is too slow in PHP. It is a lot slower than in Perl or C, the
languages I compared it to. I read and wrote out a 20 MB file in PHP,
Perl, and C (all by line). Here are my results
C: 0.938s, 0.949s, 0.945s, 0.943s
Perl: 4.946s, 2.123s, 2.119s, 2.158s
php: 15.606s,11.637s, 11.675s, 11.260s
I ran tests on 2 computers, with fairly similar results. And another
person from a forum who I asked about fgets() ran it with approximately 6
seconds in Windows and 7 in Linux for a 15 MB file.
Replacing fgets() with fread($fin, 1024) gives these results
0.835s,0.797s,0.812s,0.836s
So the problem is with fgets(). Perl is slower than C because C is
compiled and Perl is interpreted. But there isn't a reason why php should
be that much slower than Perl. And fgets() should be slower than fread()
but not by that much.
Reproduce code:
---------------
parse_p("in.txt", "out.txt");
function parse_p($in_file, $out_file)
{
$fin = fopen($in_file, "rb");
$fout = fopen($out_file, "wb");
while(!feof($fin))
{
$line = fgets($fin);
fwrite($fout, $line);
}
fclose($fin);
fclose($fout);
}
Expected result:
----------------
I expect it to be comparable to Perl or C in speed
Actual result:
--------------
It was much slower than Perl or C
--
Edit bug report at http://bugs.php.net/?id=32806&edit=1
--
Try a CVS snapshot (php4): http://bugs.php.net/fix.php?id=32806&r=trysnapshot4
Try a CVS snapshot (php5.0):
http://bugs.php.net/fix.php?id=32806&r=trysnapshot50
Try a CVS snapshot (php5.1):
http://bugs.php.net/fix.php?id=32806&r=trysnapshot51
Fixed in CVS: http://bugs.php.net/fix.php?id=32806&r=fixedcvs
Fixed in release: http://bugs.php.net/fix.php?id=32806&r=alreadyfixed
Need backtrace: http://bugs.php.net/fix.php?id=32806&r=needtrace
Need Reproduce Script: http://bugs.php.net/fix.php?id=32806&r=needscript
Try newer version: http://bugs.php.net/fix.php?id=32806&r=oldversion
Not developer issue: http://bugs.php.net/fix.php?id=32806&r=support
Expected behavior: http://bugs.php.net/fix.php?id=32806&r=notwrong
Not enough info:
http://bugs.php.net/fix.php?id=32806&r=notenoughinfo
Submitted twice:
http://bugs.php.net/fix.php?id=32806&r=submittedtwice
register_globals: http://bugs.php.net/fix.php?id=32806&r=globals
PHP 3 support discontinued: http://bugs.php.net/fix.php?id=32806&r=php3
Daylight Savings: http://bugs.php.net/fix.php?id=32806&r=dst
IIS Stability: http://bugs.php.net/fix.php?id=32806&r=isapi
Install GNU Sed: http://bugs.php.net/fix.php?id=32806&r=gnused
Floating point limitations: http://bugs.php.net/fix.php?id=32806&r=float
No Zend Extensions: http://bugs.php.net/fix.php?id=32806&r=nozend
MySQL Configuration Error: http://bugs.php.net/fix.php?id=32806&r=mysqlcfg