Haskell arrays vs lists

前端 未结 3 1696
情歌与酒
情歌与酒 2021-01-01 15:35

I\'m playing with Haskell and Project Euler\'s 23rd problem. After solving it with lists I went here where I saw some array work. This solution was much faster than mine. S

3条回答
  •  天命终不由人
    2021-01-01 16:18

    And if you're doing much indexing as well as much updating,you can

    • use Maps (or IntMaps), O(log size) indexing and update, good enough for most uses, code easy to get right
    • or, if Maps are too slow, use mutable (unboxed) arrays (STUArray from Data.Array.ST or STVectors from the vector package; O(1) indexing and update, but the code is easier to get wrong and generally not as nice.

    For specific uses, functions like accumArray give very good performance too (uses mutable arrays under the hood).

提交回复
热议问题