好慢
我覺得「把 Haskell 程式寫對」相對容易,可是「把 Haskell 程式寫快」很難… 以下是一個 2-dimensional mutable array of booleans 的型別:
import Data.Array.IO type Lattice = IOUArray (Int, Int) Bool因為稍後要頻繁地做 indexing 和 state change(是某種模擬程式),所以用 plain list 應該不是好選擇。我現在想把 n * n 個元素初始化成 random booleans,一種寫法是
{-# OPTIONS -XPatternSignatures #-} import System.Random main = do n <- readLn lat :: Lattice <- newStdGen >>= newListArray ((0, 0), (n-1, n-1)) . randoms return ()可是隨便給個
n = 1000
就要跑好幾秒,如果把 return ()
換成 getElems lat >>= print
還會 stack overflow!
--
啊,我不會寫啦!
Labels: Haskell
換台Mac Pro就會快了,啊哈 XD
Write Haskell as fast as C: exploiting strictness, laziness and recursion
Haskell as fast as C: working at a high altitude for low level performance
之前在 planet haskell 看到的
希望有所幫助 :)
喔,太好了,謝謝!:)
<< 回到主頁