
19 Mar
2013
19 Mar
'13
5:03 p.m.
Don Stewart
Here's the final program: [...]
Here is a version of the program that is just as fast:
import Prelude hiding ( getContents, foldl )
import Data.ByteString.Char8
countSpace :: Int -> Char -> Int
countSpace i c | c == ' ' || c == '\n' = i + 1
| otherwise = i
main :: IO ()
main = getContents >>= print . foldl countSpace 0
Generally speaking, I/O performance is not about fancy low-level system
features, it's about having a proper evaluation order:
| $ ghc --make -O2 -funbox-strict-fields test1 && time ./test1
| 37627064
|
| real 0m0.381s
| user 0m0.356s
| sys 0m0.023s
Versus:
| $ ghc --make -O2 -funbox-strict-fields test2 && time ./test2