Gmp*_*Gmp 1 haskell lazy-evaluation
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)
Run Code Online (Sandbox Code Playgroud)
如何判断它是否评估了内容的所有元素?
let !mapasort = rnf $ map sort content
Run Code Online (Sandbox Code Playgroud)
我使用了winghci中的行:
*Main> let !mapasort = rnf $ map sort content
Run Code Online (Sandbox Code Playgroud)
但是,返回:
*Main> mapasort ()
Run Code Online (Sandbox Code Playgroud)
谢谢
我看到两个问题:
1)为什么将maport评估为单位,().
因为rnf函数总是返回(). 请参阅文档.
2)是否评估了一切
是.列表中的DeepSeq实例(它所在的实例rnf)只调用列表中每个元素的deepseq实例:
rnf [] = ()
rnf (x:xs) = rnf x `seq` rnf xs
Run Code Online (Sandbox Code Playgroud)
您的元素都是Ints,它们具有正确的NFData实例.
我还要再添两个问题:
3)如何正确完成此基准测试?
使用标准.这里有许多标准倡导者,你可以找到可以作为搜索的好例子的答案.
4)如何将此评估强制用于非基准测试目的?
使用并行包.
import Control.Parallel.Strategies
...
let !mapsort = (map sort content) `using` (evalList rdeepseq)
Run Code Online (Sandbox Code Playgroud)
或仍在使用rnf:
let mapsort = map sort content
!_ = rnf mapsort
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1284 次 |
| 最近记录: |