How to improve the performance of Haskell IO? -
it seems haskell's io relatively slow.
for example, comparing haskell python
#io.py import sys s=sys.stdin.read() sys.stdout.write(s)
,
-- io.hs main = s <- getcontents putstr s
their performance (gen.py writes 512k data stdout):
the python version:
$ time python gen.py | python io.py > /dev/null real 0m0.203s user 0m0.015s sys 0m0.000s
the haskell version:
$ time python gen.py | runhaskell io.hs > /dev/null real 0m0.562s user 0m0.015s sys 0m0.000s
it seems haskell 1 far lower. there problem test? or inherent problem of haskell?
thanks.
your example slow because uses lazy io string
-s. both have own overheads.
in particular, string
linked list of char
-s, therefore has 2 words of space overhead each character (one word constructor tag , 1 forward pointer), , each character takes @ least 1 word (one word cached low characters, 3 words uncached characters).
strict io byte or unicode array input faster. try benchmark following:
import qualified data.bytestring b main = b.putstr =<< b.getcontents
or following:
import qualified data.text t import qualified data.text.io t main = t.putstr =<< t.getcontents
Comments
Post a Comment