How to get a program's running time in Haskell

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-03 09:28:45

问题


How can I go about getting a program's running time through system time functions in Haskell? I would like to measure the execution time of a whole program and/or an individual function.


回答1:


Assuming you don't just want to measure the total running time of your program, like so:

 $ time ./A

Then you can time a computation a number of ways in Haskell:

  • Basic timing (e.g. as in the timeit package)
  • Timing in cycles

For more statistically sound measurement, consider

  • The Criterion package.

Finally, in all cases, you need to think about lazy evaluation: do you want to measure the cost of fully evaluating whatever data you produce, or just to its outermost constructor?




回答2:


1) If you want to benchmark something, use the criterion package.

2) If you want to time a function and are positive you have controlled for laziness as needed, then just use Data.Time.getCurrentTime from the time package.:

import Data.Time
...
   start <- getCurrentTime
   runOperation
   stop <- getCurrentTime
   print $ diffUTCTime stop start

A slicker packaging of the above pattern can be found in the timeit package.

3) If you actually want the running time of a program that just happens to be written in Haskell then use your systems time utility. For most POSIX systems (Mac, Linux) just run:

$ time ./SomeProgram

And it will report user, wall, and system time.




回答3:


:set +s is really neat if use ghci, otherwise you can use Criterion.Measurement, see my answer to another question with example.




回答4:


I'm not sure how accurate it is, but using :set +s in ghci will show the time and space used for subsequent computations.



来源:https://stackoverflow.com/questions/5968614/how-to-get-a-programs-running-time-in-haskell

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!