Hello Haskellers out there!
I have the feeling that questions on performance arise more often and that the knowledge on which functions/algorithms/libraries are fast and stable is sparse.
There are of course libraries like Criterion
which allow to make measurements on one's own, and there is the profiler which can be invoked by
> ghc -O2 --make program.hs -prof -auto-all
> ./program +RTS -s
as excellently explained by @DonStewart in Tools for analyzing performance of a Haskell program
I know that:
- the use of
read
andshow
is usually a bottleneck (for theread
-function in the case of numbers there is theNumeric
package which brings a performance speedup - and there are the
Sequence
,Array
,Vector
andMap
libraries, which are often better fit to solve a problem than to use lists or nested lists Text
andBytestring
are a better option than String- I recently saw that even the use of the standard number generator slows down a program significantly and that
mwc-random
is a lot faster. - Also the answers of Python faster than compiled Haskell? revealed that the standard sorting algorithm is definitely improvable
- The usage of
Int
rather thanInteger
,BangPatterns
and strict folds often yield an increase in performance - There are
conduit
andpipes
to "stricten" IO, (which I have to admit I haven't used yet) - type signatures are general an improvement
What are other common pitfalls and bottlenecks one tends to use?
How to solve those?
The topics that come to my mind are:
- functions
- data structures
- algorithms
- LANGUAGE extensions (for GHC)?
- compiler options?