[Haskell-cafe] Streaming bytes and performance

Peter Simons simons at cryp.to
Tue Mar 19 22:27:39 CET 2013


Hi Don,

 > Compare your program (made lazy) on lazy bytestrings using file IO: [...]

if I make those changes, the program runs even faster than before:

  module Main ( main ) where

  import Prelude hiding ( foldl, readFile )
  import Data.ByteString.Lazy.Char8

  countSpace :: Int -> Char -> Int
  countSpace i c | c == ' ' || c == '\n' = i + 1
                 | otherwise             = i

  main :: IO ()
  main = readFile "test.txt" >>= print . foldl countSpace 0

This gives

 | $ ghc --make -O2 -funbox-strict-fields test1 && time ./test1
 | 37627064
 |
 | real    0m0.375s
 | user    0m0.346s
 | sys     0m0.028s

versus:

 | $ ghc --make -O2 -funbox-strict-fields test2 && time ./test2
 | 37627064
 |
 | real    0m0.324s
 | user    0m0.299s
 | sys     0m0.024s

Whether getFile or getContents is used doesn't seem to make difference.

Take care,
Peter



More information about the Haskell-Cafe mailing list