[Haskell-beginners] Improving Performance

Ertugrul Söylemez es at ertes.de
Tue Jun 19 22:25:05 CEST 2012

Robert Heumüller <mailing at heum.de> wrote:

> I've added the type signatures. What's next?
> http://privatepaste.com/26073e4b0e

Great, now that makes it much easier to reason about performance.  A
quick look at the type signatures tells me that your next step is to use
the right tool for the job.  In Haskell this basically means to use the
right data and control structures.

First of all know when not to use lists.  A good intuition is to ask
yourself whether the lists will actually live in memory.  If no, then
lists are fine.  If yes (like in your case), examine your usage.  If you
use them like stacks (working on the head only) that's probably fine
(it's not in your case).  However, if you fold and unfold a lot or index
individual elements you probably want a different data structure like a
vector or a map.  This is a somewhat involved topic and needs a lot of
training, but making the right decision here will give you the main
performance boost.

Note:  Whatever you do, stay in pure land.  Always use immutable data
structures.  If your code is too slow, you may be using the right data
structure the wrong way.  Going mutable is almost always the wrong way.
Again: It takes some training, especially in Haskell where (as a
beginner) you may not see directly what is actually happening.

Also find spots where sharing can be used.  Unshared example:

    f x' + f x'

Shared version:

    let x = f x' in x + x

Haskell compilers don't do that automatically for you, because sharing
trades memory for run-time performance, and the outcome is hard to
predict for a compiler.  I suggest experimenting.


Not to be or to be and (not to be or to be and (not to be or to be and
(not to be or to be and ... that is the list monad.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 836 bytes
Desc: not available
URL: <http://www.haskell.org/pipermail/beginners/attachments/20120619/cff0a484/attachment.pgp>

More information about the Beginners mailing list