[Haskell-cafe] Streaming bytes and performance
Peter Simons
simons at cryp.to
Tue Mar 19 22:03:04 CET 2013
Don Stewart <dons00 at gmail.com> writes:
> 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 <test.txt
| Linking test2 ...
| 37627064
|
| real 0m0.383s
| user 0m0.316s
| sys 0m0.065s
Using this input file stored in /dev/shm:
| $ ls -l test.txt
| -rw-r--r-- 1 simons users 208745650 Mar 19 21:40 test.txt
Take care,
Peter
More information about the Haskell-Cafe
mailing list