in reply to Last N lines from file (tail)

I'm horrified to see so many calls to sysread(). The whole point of sysread() is to bypass buffering and go to the OS. And system calls are slow -- slow enough to notice when you're making hundreds and hundreds of them.

In other words: DON'T DO THAT.

    -- Chip Salzenberg, Free-Floating Agent of Chaos

Replies are listed 'Best First'.
Re: EEK! sysread() is expensive!
by SpongeBob (Novice) on Dec 22, 2001 at 01:52 UTC
    I'm just wondering if your reply relates to my answer also. I changed the sysread and sysseek calls to plain read and seek (and removed that last seek), and benchmarked my old and new versions, and got a huge performance hit (~3 cycles/sec with sys* calls, ~1100 cps w/o sys).
    Update: And I checked clintp's answer with & w/o sys* calls, and it was ~5 cps with and ~4 w/o. This was all reading the last 400 lines from a 1000 line file, 10 bytes/line.
      Well, sure, because a plain read() would have used stdio, which probably translated to the equivalant of sysread(4096) at least. If you're doing N reads of one byte, then change that to N reads of 4096 bytes, of course you'll have a slowdown ... and a wasteful one, since every time you read a block you use only one byte.

          -- Chip Salzenberg, Free-Floating Agent of Chaos