sysread vs read?

Discussion in 'Perl Misc' started by kj, Jul 10, 2007.

  1. kj

    kj Guest

    I've rtfm'd this question but I still can't figure out why one
    would prefer Perl's read over Perl's sysread, or viceversa. Is
    one significantly faster than the other?

    (And why would Perl provide two different functions that, aside
    from possible differences in performance, seem to both do pretty
    much the same thing?)

    Any words o' wisdom would be much appreciated.

    TIA!

    kj

    --
    NOTE: In my address everything before the first period is backwards;
    and the last period, and everything after it, should be discarded.
    kj, Jul 10, 2007
    #1
    1. Advertising

  2. kj

    Guest

    kj <> wrote:
    > I've rtfm'd this question but I still can't figure out why one
    > would prefer Perl's read over Perl's sysread, or viceversa.


    "read" can be used with readline (aka "<$fh>") , while sysread generally
    can't be, at least not without introducing strange behavior.

    sysread can be used with select (or IO::Select), while "read" generally
    (or at least often) can't be.


    > Is
    > one significantly faster than the other?


    If you use very small read sizes, then read will be much faster than
    sysread. If you don't use small read sizes, then the main difference is in
    what other functions/operations they are compatible with.


    > (And why would Perl provide two different functions that, aside
    > from possible differences in performance, seem to both do pretty
    > much the same thing?)


    It would have been very hard to make readline compatible with sysread
    (and select) without making readline very slow. So we have two
    incompatible sets of features. "read" and "sysread" or analogous to each
    other, but one in the buffered camp and one in the unbuffered camp.

    Xho

    --
    -------------------- http://NewsReader.Com/ --------------------
    Usenet Newsgroup Service $9.95/Month 30GB
    , Jul 11, 2007
    #2
    1. Advertising

  3. kj

    kj Guest

    In <20070710192114.167$> writes:

    >kj <> wrote:
    >> I've rtfm'd this question but I still can't figure out why one
    >> would prefer Perl's read over Perl's sysread, or viceversa.


    >"read" can be used with readline (aka "<$fh>") , while sysread generally
    >can't be, at least not without introducing strange behavior.


    Hmmm... I sense that I'm missing something important here...

    The reason I'm fussing over read and sysread in the first place is
    that I wanted to speed up some code that currently uses readline
    to get data from a huge file (about 1.5GB). I thought that I could
    speed things up by reducing the number of disk reads; i.e. by
    periodically reading, say, 4096 bytes to a buffer in memory and
    feeding lines to the program from this buffer...

    But, reading your reply between the lines a bit, I'm now guessing
    that maybe Perl already does precisely this behind the scenes even
    when one uses readline, meaning that I would not be gaining much
    speed, and may even lose some, by switching from my naive <$fh>
    implementation to one using read or sysread. Did I get this right?

    TIA!

    kj

    --
    NOTE: In my address everything before the first period is backwards;
    and the last period, and everything after it, should be discarded.
    kj, Jul 11, 2007
    #3
  4. kj

    Guest

    kj <> wrote:
    > In <20070710192114.167$> writes:
    >
    > >kj <> wrote:
    > >> I've rtfm'd this question but I still can't figure out why one
    > >> would prefer Perl's read over Perl's sysread, or viceversa.

    >
    > >"read" can be used with readline (aka "<$fh>") , while sysread generally
    > >can't be, at least not without introducing strange behavior.

    >
    > Hmmm... I sense that I'm missing something important here...
    >
    > The reason I'm fussing over read and sysread in the first place is
    > that I wanted to speed up some code that currently uses readline
    > to get data from a huge file (about 1.5GB). I thought that I could
    > speed things up by reducing the number of disk reads; i.e. by
    > periodically reading, say, 4096 bytes to a buffer in memory and
    > feeding lines to the program from this buffer...


    The disk reads issue is mostly likely at yet another level, well below
    Perl. Even sysread is not likely to be issuing a physical disk-read for
    each read.


    > But, reading your reply between the lines a bit, I'm now guessing
    > that maybe Perl already does precisely this behind the scenes even
    > when one uses readline, meaning that I would not be gaining much
    > speed, and may even lose some, by switching from my naive <$fh>
    > implementation to one using read or sysread. Did I get this right?


    Yes, read and readline already do chunking.

    Xho

    --
    -------------------- http://NewsReader.Com/ --------------------
    Usenet Newsgroup Service $9.95/Month 30GB
    , Jul 11, 2007
    #4
  5. kj

    kj Guest

    In <20070711124042.743$> writes:
    >kj <> wrote:
    >> But, reading your reply between the lines a bit, I'm now guessing
    >> that maybe Perl already does precisely this behind the scenes even
    >> when one uses readline, meaning that I would not be gaining much
    >> speed, and may even lose some, by switching from my naive <$fh>
    >> implementation to one using read or sysread. Did I get this right?


    >Yes, read and readline already do chunking.


    Cool. Thanks!

    kj

    --
    NOTE: In my address everything before the first period is backwards;
    and the last period, and everything after it, should be discarded.
    kj, Jul 12, 2007
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Hal Fulton

    sysread and buffered I/O

    Hal Fulton, Jul 21, 2004, in forum: Ruby
    Replies:
    39
    Views:
    631
    Tanaka Akira
    Jul 24, 2004
  2. William E. Rubin
    Replies:
    3
    Views:
    109
    William E. Rubin
    Dec 8, 2005
  3. Daniel Berger
    Replies:
    4
    Views:
    207
    Daniel Berger
    Sep 12, 2008
  4. J. Romano

    Perl's read() vs. sysread()

    J. Romano, Feb 6, 2004, in forum: Perl Misc
    Replies:
    3
    Views:
    2,073
    Walter Roberson
    Feb 6, 2004
  5. Yohan N. Leder

    Read socket using both <> and sysread()

    Yohan N. Leder, Aug 8, 2006, in forum: Perl Misc
    Replies:
    13
    Views:
    305
    Yohan N. Leder
    Aug 12, 2006
Loading...

Share This Page