in reply to Re^4: Speed reading (files)
in thread Speed reading (files)

Good question. I've heard it many times of the years, but I can't say where. Maybe it's when the handle is not a file handle. FreeBSD's man 2 read supports this theory: "The system guarantees to read the number of bytes requested if the descriptor references a normal file that has that many bytes left before the end-of-file, but in no other case."

btw, [doc://sysread] works better than [sysread].

Replies are listed 'Best First'.
Re^6: Speed reading (files)
by BrowserUk (Patriarch) on Aug 05, 2005 at 05:23 UTC

    Great. That clarified things. On a non-file handle, read will wait for more input to become available if the buffer doesn't contain enough to satisfy the request, whereas sysread returns what is available without waiting (subject to EOLs):

    P:\test>p1 perl> print read STDIN, $in, 10; print "'$in'";; 1234 5678 10 '1234 5678 ' perl> print sysread STDIN, $in, 10; print "'$in'";; 1234 6 '1234 '

    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
    "Science is about questioning the status quo. Questioning authority".
    The "good enough" maybe good enough for the now, and perfection maybe unobtainable, but that should not preclude us from striving for perfection, when time, circumstance or desire allow.
      As an aside to this conversation, sysread is noticeably faster than read, using my same simple test. The larger the file, the bigger the difference.
      read 0.60891580581665 0.601351976394653 0.600950956344604 0.644972085952759 0.650673866271973 sysread 0.409609079360962 0.405777931213379 0.409843921661377 0.426880121231079 0.432134866714478

        Is that with or without binmode or :raw applied?


        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
        "Science is about questioning the status quo. Questioning authority".
        The "good enough" maybe good enough for the now, and perfection maybe unobtainable, but that should not preclude us from striving for perfection, when time, circumstance or desire allow.