我有一個非常簡單的函數f :: Int -> Int
,我想寫一個程序,每個n = 1,2,...,max
調用f
。在每次撥打電話f
後,應該顯示該點以前使用的(累計)時間(以及n
和f n
)。這如何實現?函數的重複計時
我還是很新的,以在Haskell輸入/輸出,所以這是我到目前爲止已經試過(使用一些玩具例子功能f
)
f :: Int -> Int
f n = sum [1..n]
evalAndTimeFirstN :: Int -> Int -> Int -> IO()
evalAndTimeFirstN n max time =
if n == max
then return() -- in the following we have to calculate the time difference from start to now
else let str = ("(" ++ (show n) ++ ", " ++ (show $ f n) ++ ", "++ (show time)++ ")\n")
in putStrLn str >> evalAndTimeFirstN (n+1) max time -- here we have to calculate the time difference
main :: IO()
main = evalAndTimeFirstN 1 5 0
我不太怎麼看我必須在這裏介紹時間。 (Int
對於time
可能必須用別的東西來代替。)
將基準委託給專門的工具可能會更好, http://www.serpentine.com/criterion/ – karakfa
謝謝你的鏈接!現在對我來說似乎有點太難了,所以我認爲在使用這樣的庫之前,我仍然試圖堅持一種基本的方法,因爲我想學習如何做這樣的想法。 – flawr
以懶惰的語言很難做到正確和雙重的基準測試。如果你想得到準確的結果,我不建議你自己滾動。 –