使用Criterion键入错误

时间:2010-10-27 12:25:05

标签: haskell criterion

我阅读了文档和一些谈论这个软件包的文章,但我是Haskell的新手并且对我的理解并不多,但我尝试了......

以下是我的所作所为:

module Main where  
{-# LANGUAGE BangPatterns #-}   
import Control.Parallel(par,pseq)  
import Control.Exception  
import Data.List  
import IO  
import Data.Char  
import Criterion.Main (defaultMain, bench)  

learquivo :: FilePath -> IO ([[Int]])  
learquivo "mkList1.txt"  = do   
    conteudo <- readFile "mkList1.txt" 
    return (read conteudo) 


main = defaultMain [  
    bench "map sort learquivo" $ \n -> map sort learquivo
    ]

因为它发生了以下错误:

Couldn't match expected type [[a]]
       against inferred type FilePath -> IO [[Int]]

2 个答案:

答案 0 :(得分:2)

问题是:map sort learquivo

sort需要一个列表,因此map sort需要一个列表列表([[a]]),而learquivo的类型为FilePath -> IO [[Int]]类型。

您可能需要以下内容:

main = do
    contents <- learquivo "mkList1.txt"
    defaultMain [
       bench "map sort learquivo" $ \n -> map sort contents
    ]

您的代码中有各种各样的东西可以清理,但这应该可以帮助您。

答案 1 :(得分:2)

通过使用nfwhnf函数,我通常会运行它,我会给出我的代码:

import Data.List
import Criterion.Main

main :: IO ()
main = do
   -- content <- learquivo "mkList1.txt"  
   let content = [ [big, big - step.. 0] | big <- [1000..1010], step <- [1..5]] :: [[Int]]
   defaultMain
        [ bench "benchmark-name" (nf (map sort) content)]

编辑:如果你喜欢这个,那么也可以试一试:

module Main where

import Data.List
import Criterion.Main
import Criterion.Config
import Criterion.MultiMap as M

main :: IO ()
main = do
   let myConfig = defaultConfig {
              -- Always display an 800x600 window with curves.
              cfgPlot = M.singleton KernelDensity (Window 800 600)
              }
   let content = [ [big, big-step.. 0] | big <- [1000..1010], step <- [1..5]] :: [[Int]]
   defaultMainWith myConfig (return ())
        [ bench "benchmark-name" (nf (map sort) content)]
相关问题