note 97668 added to function.fgets
| From: | gurdiga at osu1 dot php dot net | Date: | Sun, 02 May 2010 07:39:50 +0000 |
| Subject: | note 97668 added to function.fgets | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-168848@lists.php.net to get a copy of this message | ||
Regarding "For large files, consider using stream_get_line rather than fgets - it can make a
significant difference.", I've run a couple of tests with the command lines given as
examples and, yes, in that specific case fgets is slower.
But, when I've removed the line length (the second argument) from fgets call, the code got even
faster than stream_get_line.
Here are my results:
$ 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);'
real 0m0.654s
user 0m0.637s
sys 0m0.062s
$ time yes "This is a test line" | head -1000000 | php -r
'$fp=fopen("php://stdin","r"); while($line=fgets($fp,65535)) { 1; }
fclose($fp);'
real 0m6.134s
user 0m6.108s
sys 0m0.051s
$ time yes "This is a test line" | head -1000000 | php -r
'$fp=fopen("php://stdin","r"); while($line=fgets($fp)) { 1; }
fclose($fp);'
real 0m0.408s
user 0m0.411s
sys 0m0.048s
Nice! :)
----
Server IP: 89.28.118.75
Probable Submitter: 89.28.117.120
----
Manual Page -- http://www.php.net/manual/en/function.fgets.php
Edit -- https://master.php.net/note/edit/97668
Del: integrated -- https://master.php.net/note/delete/97668/integrated
Del: useless -- https://master.php.net/note/delete/97668/useless
Del: bad code -- https://master.php.net/note/delete/97668/bad+code
Del: spam -- https://master.php.net/note/delete/97668/spam
Del: non-english -- https://master.php.net/note/delete/97668/non-english
Del: in docs -- https://master.php.net/note/delete/97668/in+docs
Del: other reasons-- https://master.php.net/note/delete/97668
Reject -- https://master.php.net/note/reject/97668
Search -- https://master.php.net/manage/user-notes.php