[英]how to force the evaluation of this expression?
module Main where
import Control.Parallel(par,pseq)
import Text.Printf
import Control.Exception
import System.CPUTime
import Data.List
import IO
import Data.Char
import Control.DeepSeq
time :: IO t -> IO t
time a = do
start <- getCPUTime
v <- a
end <- getCPUTime
let diff = (fromIntegral (end - start)) / (10^12)
printf "Computation time: %0.3f sec\n" (diff :: Double)
return v
learquivo :: FilePath -> IO ([[Int]])
learquivo s = do
content <- readFile s
return (read content)
main :: IO ()
main = do
t5 <- getCPUTime
content <- learquivo "mkList1.txt"
let !mapasort = rnf $ map sort content
t6 <- getCPUTime
let diffft6t5 = (fromIntegral (t6 - t5)) / (10^12)
printf "Computation time Mapasort: %0.3f sec\n" (diffft6t5 :: Double)
如何判斷它是否評估內容的所有元素?
let !mapasort = rnf $ map sort content
我在winghci中使用了該行:
*Main> let !mapasort = rnf $ map sort content
但是,返回:
*Main> mapasort ()
謝謝
我看到兩個問題:
1)為什么mapsort評估為單位()
。
因為rnf
函數總是返回()
。 請參閱文檔 。
2)一切都經過評估
是。 list的DeepSeq實例(rnf所在的rnf
)僅為列表中的每個元素調用deepseq實例:
rnf [] = ()
rnf (x:xs) = rnf x `seq` rnf xs
您的元素都是Ints,它們具有正確的NFData實例。
我還要再添加兩個問題:
3)如何正確進行基准測試?
使用標准 。 SO上有許多Criterion倡導者,您可以找到可以作為搜索示例的好答案。
4)出於非基准目的,應如何強制執行此評估?
使用並行包。
import Control.Parallel.Strategies
...
let !mapsort = (map sort content) `using` (evalList rdeepseq)
或仍在使用rnf
:
let mapsort = map sort content
!_ = rnf mapsort
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.