In Haskell, how can i 'simply' measure a functions performance. For example, how long it takes to run, or how much memory it takes?. I am aware of profiling, however, is there a more simple way that will not require me to change my code too much?
问题:
回答1:
Measuring how long it takes to run and how much memory it takes are two separate problems, namely: benchmarking and profiling. Haskell has a well defined set of tools for both. Solving neither of the problems requires you to make any changes to the actual application's code.
Benchmarking
This is done using libraries. There is an ultimate winner in that area, which was suggested by Niklas in the comments, namely Criterion. The library is very well designed, isn't hard to use and produces a very detailed data.
The workflow is the following: you create a separate module containing the setup of your benchmark, compile it and run it with options. To get a reference on available options run it with --help
modifier.
You can find examples of setup modules here.
Profiling
There is enough of good materials on that already, so I'll just refer to them:
- General reference on profiling
- A tutorial in Real World Haskell
- A tutorial on profiling with Cabal
回答2:
For extremely crude information on how individual functions perform compared to each other, you can use ghci
Prelude> :set +s
Prelude> last [1..100000000]
100000000
(1.65 secs, 4000685276 bytes)
You need to be aware that ghci doesn't compile code, so runs much slower than ghc, the timing and memory usage data is approximate, and that absolutely no optimisation has been performed.
This means that it gives you only a very rough idea of how (in)efficient your code is, and is no substitute for proper benchmarking and profiling of compiled and optimised code, as detailed in Nikita Volkov's answer.