[Haskell-cafe] Re: Silly I/O question
alastair at reid-consulting-uk.ltd.uk
Tue Sep 28 18:57:55 EDT 2004
On Tuesday 28 September 2004 22:19, John Goerzen wrote:
> [program that calls isEOF once per line deleted]
> but it seems wasteful to poll isEOF so much.
I think all Haskell implementations have a buffering layer between the Haskell
level and the operating system. So, calls to hGetLine, hIsEOF, etc. don't
make (expensive) calls to the operating system but, instead, make (cheap)
function calls to the I/O library to examine the state of the buffer for that
file. In other words, calling isEOF is pretty cheap.
That said, if you want to write a cat-like program which is as fast as Unix
cat, you should not process data a character at a time or a line at a time
but, rather, read fixed size blocks. Ideally the block size would match what
the OS can provide efficiently and you would avoid introducing additional
layers of buffering. You would also avoid converting from the external
representation (a sequence of bytes in memory) to some internal
representation (a linked list of characters, an array of unboxed values, or
whatever) since you will waste a lot of time in conversion.
ps It sounds like you're trying to learn Haskell by writing programs with lots
of I/O in them. This isn't really playing to Haskell's strengths and forces
you to learn some tricky stuff (and, if chasing performance, learn some
murky, non-portable libraries) before you learn what Haskell is really good
More information about the Haskell-Cafe