Haskell计算函数执行的时间 [英] Haskell calculate time of function performing

查看:294
本文介绍了Haskell计算函数执行的时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试着编写代码来计算函数花费的时间

pre $ list< - buildlist 10000 10000
starttime < - getClockTime
let sortedlist = quicksort list
endtime< - getClockTime
let difftime = diffClockTimes endtime starttime


$ b $ p function buildlist:

  buildlist :: Int  - > Int  - > IO [Int] 
buildlist nm = do
种子< - getStdGen
let l = randomRs(0,m)seed
let list = take nl
return list

function quicksort:

  quicksort [] = [] 
quicksort(x:xs)=
let head = [a | a< -xs,a< = x]
tail = [a | a< -xs,a> x]
in quicksort head ++ [x] ++ quicksort tail

第一个问题:当我输出difftime时,无论列表多长时间,它总是为零。



第二个:我想知道在> =

  getClockTime>> =真实世界Haskell 的意思。 (\(TOD sec _) - > return sec)

strong>我写这个来从 tdSec tdPicosec TimeDiff 变量。
是否有更简单的方法?

  time< ;-( \(TimeDiff _ _ _ _ _ s ps ) - > return [(\ a  - > fromIntegral a :: Double)s,(\ a  - > fromIntegral a :: Double)ps])difftime 


解决方案

问题1:



列表!它只是将 sortedlist 这个名称定义为 quicksort list ,但这个不会被计算,直到实际需要这个值。这是懒惰的评价。我们不用这种语言做额外的工作。



由于基准测试是额外无用的工作(这就是要点),所以这使基准测试变得困难。 b

您的选择




  • 使用 seq seq 的类型为 a - > b - > b 并且具有评估其第一个参数到所谓的弱头标准形式的行为。这里既然你想强制整个列表,你可能需要使用 deepseq

  • 使用适当的基准测试软件包,比如标准(优先和更容易)


问题2:



>> = 是一元绑定运算符。这里需要 IO a 类型的 IO 动作和一个函数 a - > IO b 并将它们组合在一起以创建类型为 IO b 的新动作。这和记谱法是一样的。 foo>> = \ x - > expr

  do x < -  foo 
expr $ b是一样的$ b

实际上, do 表示法只是语法糖对于>> =



我不确定问题3中提问的内容 - 应该得到它自己的Stackoverflow问题。


i tried to code to calculate time that a function costs

list <- buildlist 10000 10000    
starttime <- getClockTime
let sortedlist = quicksort list
endtime <- getClockTime
let difftime = diffClockTimes endtime starttime

function buildlist:

buildlist :: Int -> Int -> IO [Int]
buildlist n m = do
    seed <- getStdGen
    let l = randomRs (0, m) seed
    let list = take n l
    return list

function quicksort:

quicksort [] = []
quicksort (x:xs) =
    let head = [a|a<-xs,a<=x]
        tail = [a|a<-xs,a>x]
    in quicksort head ++ [x] ++ quicksort tail

first question: when i output the difftime, it is always zero no matter how long the list is.

second one: i wonder what ">>=" in the code from Real World Haskell means.

getClockTime >>= (\(TOD sec _) -> return sec)

third: i write this to get tdSec and tdPicosec from a TimeDiff variable. is there any easier way?

time <-(\(TimeDiff _ _ _ _ _ s ps) -> return [ ( \a -> fromIntegral a :: Double ) s , ( \a -> fromIntegral a :: Double ) ps ] ) difftime

解决方案

Question 1:

Your code does not sort the list! It simply defines the name sortedlist as being quicksort list but this wont be computed until the value is actually needed. That is lazy evaluation. We do not do extra work in this language.

Since benchmarks are extra useless work (that is the point), this makes benchmarking hard.

Your choices

  • Use seq. seq has type a -> b -> b and has the behavior of evaluating its first argument to what is called "weak head normal form". Here since you want to force the entire list you might want to use deepseq
  • Use a proper benchmarking package like criterion (prefered and easier)

Question 2:

>>= is the monadic bind operator. Here it takes an IO action of type IO a and a function a -> IO b and combines them together to make a new action of type IO b. This does the same thing as do notation. foo >>= \x -> expr is the same thing as

 do x <- foo
    expr

and in fact, do notation is just syntactic sugar for >>=

I'm not sure what is being asked in question 3--perhaps it should get its own Stackoverflow question.

这篇关于Haskell计算函数执行的时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆