As part of a programming challenge, I need to read, from stdin, a sequence of space-separated integers (on a single line), and print the sum of those integers to stdout
Read is slow
Fast read, from this answer, will bring you down to 5.5 seconds.
import Numeric
fastRead :: String -> Int
fastRead s = case readDec s of [(n, "")] -> n
Strings are Linked Lists
In Haskell the String
type is a linked list. Using a packed representation (bytestring
if you really only want ascii but Text
is also very fast and supports unicode). As shown in this answer, the performance should then be neck and neck.