note 113113 added to function.fgets
| From: | DadeBrandon | Date: | Sat, 31 Aug 2013 00:34:01 +0000 |
| Subject: | note 113113 added to function.fgets | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-196133@lists.php.net to get a copy of this message | ||
Regarding Leigh Purdie's comment (from 4 years ago) about stream_get_line being better for
large files, I decided to test this in case it was optimized since then and I found out that
Leigh's comment is just completely incorrect
fgets actually has a small amount of better performance, but the test Leigh did was not set up to
produce good results
The suggested test was:
$ time yes "This is a test line" | head -1000000 | php -r
'$fp=fopen("php://stdin","r");
while($line=stream_get_line($fp,65535,"\n")) { 1; } fclose($fp);'
0m1.616s
$ time yes "This is a test line" | head -1000000 | php -r
'$fp=fopen("php://stdin","r"); while($line=fgets($fp,65535)) { 1; }
fclose($fp);'
0m7.392s
The reason this is invalid is because the buffer size of 65535 is completely unnecessary
piping the output of "yes 'this is a test line'" in to PHP makes each line 19
characters plus the delimiter
so while I don't know why stream_get_line performs better with an oversize buffer, if both
buffer sizes are correct, or default, they have a negligable performance difference - although
notably, stream_get_line is consistent - however if you're thinking of switching, make sure to
be aware of the difference between the two functions, that stream_get_line does NOT append the
delimiter, and fgets DOES append the delimiter
Here are the results on one of my servers:
Buffer size 65535
stream_get_line: 0.340s
fgets: 2.392s
Buffer size of 1024
stream_get_line: 0m0.348s
fgets: 0.404s
Buffer size of 8192 (the default for both)
stream_get_line: 0.348s
fgets: 0.552s
Buffer size of 100:
stream_get_line: 0.332s
fgets: 0.368s
----
Server IP: 69.147.83.199
Probable Submitter: 96.49.186.163
----
Manual Page -- http://php.net/manual/en/function.fgets.php
Edit -- https://master.php.net/note/edit/113113
Del: integrated -- https://master.php.net/note/delete/113113/integrated
Del: useless -- https://master.php.net/note/delete/113113/useless
Del: bad code -- https://master.php.net/note/delete/113113/bad+code
Del: spam -- https://master.php.net/note/delete/113113/spam
Del: non-english -- https://master.php.net/note/delete/113113/non-english
Del: in docs -- https://master.php.net/note/delete/113113/in+docs
Del: other reasons-- https://master.php.net/note/delete/113113
Reject -- https://master.php.net/note/reject/113113
Search -- https://master.php.net/manage/user-notes.php