As part of the programming task, I need to read from stdin a sequence of integers separated by spaces (on one line) and print the sum of these integers in stdout. This sequence can contain as many as 10,000,000 integers.
I have two solutions for this: one is written in Haskell ( foo.hs ), and the other is equivalent, written in Python 2 ( foo.py ). Unfortunately, the (compiled) Haskell program is consistently slower than the Python program, and I find it difficult to explain the performance mismatch between the two programs; See the Benchmark section below. Anyway, I expected Haskell to have the strength ...
What am I doing wrong? How can I explain this discrepancy? Is there an easy way to speed up my Haskell code?
(For information, I'm using a mid-2010 MacBook Pro with 8GB RAM, GHC 7.8.4, and Python 2.7.9.)
foo.hs
main = print . sum =<< getIntList getIntList :: IO [Int] getIntList = fmap (map read . words) getLine
(compiled with ghc -O2 foo.hs )
foo.py
ns = map(int, raw_input().split()) print sum(ns)
Benchmark
Further test.txt consists of one line of 10 million integers, separated by spaces.
performance python io haskell
Jubobs Mar 21 '15 at 18:38 2015-03-21 18:38
source share