Skip to content

Instantly share code, notes, and snippets.

@joseluisq
Last active March 17, 2022 07:09
Show Gist options
  • Select an option

  • Save joseluisq/6ee3876dc64561ffa14b to your computer and use it in GitHub Desktop.

Select an option

Save joseluisq/6ee3876dc64561ffa14b to your computer and use it in GitHub Desktop.

Revisions

  1. joseluisq revised this gist Aug 18, 2016. 1 changed file with 0 additions and 2 deletions.
    2 changes: 0 additions & 2 deletions stream_get_line.md
    Original file line number Diff line number Diff line change
    @@ -40,5 +40,3 @@ Buffer size of 100:
    stream_get_line: 0.332s
    fgets: 0.368s
    ```

    Source: http://ie2.php.net/manual/en/function.fgets.php#113113
  2. joseluisq revised this gist Aug 18, 2016. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion stream_get_line.md
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,4 @@
    # Comparative between PHP `stream_get_line` and `fgets` about large files processing
    # Comparative between PHP `stream_get_line` and `fgets` about processing large files

    > Source: http://ie2.php.net/manual/en/function.fgets.php#113113
  3. joseluisq revised this gist Aug 18, 2016. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion stream_get_line.md
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,4 @@
    # Testing about PHP `stream_get_line` and `fgets` for large files
    # Comparative between PHP `stream_get_line` and `fgets` about large files processing

    > Source: http://ie2.php.net/manual/en/function.fgets.php#113113
  4. joseluisq revised this gist Aug 18, 2016. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions stream_get_line.md
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,6 @@
    # Testing about PHP `stream_get_line` and `fgets` for large files

    > From http://ie2.php.net/manual/en/function.fgets.php#113113
    > Source: http://ie2.php.net/manual/en/function.fgets.php#113113
    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.
    @@ -41,4 +41,4 @@ stream_get_line: 0.332s
    fgets: 0.368s
    ```

    Source http://ie2.php.net/manual/en/function.fgets.php#113113
    Source: http://ie2.php.net/manual/en/function.fgets.php#113113
  5. joseluisq revised this gist Mar 9, 2016. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion stream_get_line.md
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,4 @@
    # Testing about PHP `stream_get_line` for process large files
    # Testing about PHP `stream_get_line` and `fgets` for large files

    > From http://ie2.php.net/manual/en/function.fgets.php#113113
  6. joseluisq revised this gist Mar 9, 2016. 1 changed file with 5 additions and 9 deletions.
    14 changes: 5 additions & 9 deletions stream_get_line.md
    Original file line number Diff line number Diff line change
    @@ -1,9 +1,9 @@
    # Testing about PHP `stream_get_line` for process large files

    > From http://ie2.php.net/manual/es/function.fgets.php#113113
    > From http://ie2.php.net/manual/en/function.fgets.php#113113
    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.
    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:

    @@ -19,11 +19,7 @@ $ time yes "This is a test line" | head -1000000 | php -r '$fp=fopen("php://stdi
    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
    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:

    @@ -45,4 +41,4 @@ stream_get_line: 0.332s
    fgets: 0.368s
    ```

    Source http://ie2.php.net/manual/es/function.fgets.php#113113
    Source http://ie2.php.net/manual/en/function.fgets.php#113113
  7. joseluisq created this gist Mar 9, 2016.
    48 changes: 48 additions & 0 deletions stream_get_line.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,48 @@
    # Testing about PHP `stream_get_line` for process large files

    > From http://ie2.php.net/manual/es/function.fgets.php#113113
    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:

    ```sh
    $ 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
    ```

    ```sh
    $ 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:

    ```sh
    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
    ```

    Source http://ie2.php.net/manual/es/function.fgets.php#113113