[Haskell-cafe] Shootout favoring imperative code
sebastian.sylvan at gmail.com
Thu Jan 5 13:39:47 EST 2006
On 1/5/06, Chris Kuklewicz <haskell at list.mightyreason.com> wrote:
> Also about sum-file: They do not reveal what the actual 8k test file
> contains. So there is no way to reproduce the benchmark locally for
> testing. (One can learn it totals 400000, but since negative numbers
> are allowed, this does not help much).
> The problem can even be solved in one line with (g)awk.
> Apparantly it is bottlenecked by parsing strings into integers, but they
> specify "Programs should use built-in line-oriented I/O functions rather
> than custom-code." which means the programmer's hands are completely
> tied. So it is just a benchmark of the build-in library function, not
> of any algorithm the programmer provides.
> There is no need to beat a dead horse, though. This benchmark sets out
> to test fgets / atoi, and that is all. There are better benchmarks to
> spend time on.
I agree. The benchmark really is about how fast you can call low-level
IO system calls. But since Haskell is a high-level language it's
natural that it's a bit difficult to get access to these unsafe (but
fast) low-level functions.
In fact, if I really wanted to do this, I would use the FFI...
Do you think they'll accept this contribution for sum-file?
foreign import ccall "stdio.h" fgets :: CString -> Int -> Ptr CFile ->IO CString
foreign import ccall safe "stdlib.h" atoi :: CString -> Int
foreign import ccall safe "stdio.h &(*stdin)" c_stdin :: Ptr CFile
bufferSize = 128
loop :: CString -> Int -> IO Int
loop buf v =
do ret <- fgets buf bufferSize c_stdin
case (ret == nullPtr) of
True -> return v -- eof, or some other error
False -> do let i = atoi buf
i `seq` loop buf (v + i) -- force eval of 'i'!
main = do v <- allocaArray bufferSize (\buf -> loop buf 0)
More information about the Haskell-Cafe