Why is this Haskell program so much slower than an equivalent Python one?

前端 未结 3 1456
春和景丽
春和景丽 2021-02-03 17:52

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

3条回答
  •  既然无缘
    2021-02-03 18:19

    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.

提交回复
热议问题