It seems that Haskell's IO is relatively slow.
For example, comparing Haskell with Python
main = do
s <- getContents
Their performance (gen.py writes 512k data into stdout):
The Python version:
$ time python gen.py | python io.py > /dev/null
The Haskell version:
$ time python gen.py | runhaskell io.hs > /dev/null
It seems that the Haskell one is far lower. Is there any problem with my test? Or is it just the inherent problem of Haskell?
Best How To :
Your example is slow because it uses lazy IO with
String-s. Both have their own overheads.
String is a linked list of
Char-s, therefore it has two words of space overhead for each character (one word for the constructor tag and one for the forward pointer), and each character takes up at least one word (one word for cached low characters, three words for uncached characters).
Strict IO with byte or unicode array input is much faster. Try to benchmark the following:
import qualified Data.ByteString as B
main = B.putStr =<< B.getContents
Or the following:
import qualified Data.Text as T
import qualified Data.Text.IO as T
main = T.putStr =<< T.getContents