Lazy Evaluation

I’ve recently been learning the Haskell programming language which has some very interesting concepts, one being lazy evaluation. Lazy evaluation defers evaluating any piece of code right until the value is actually needed rather than evaluating it in the order in which it is defined as with most programming languages. One immediate benefit of this is that conditional execution benefits from only evaluating one side of the branch without having to explicitly code in a short circuit that most languages rely on.

This seemed to offer an optimisation opportunity for my genetic programming library which currently evaluates both sides of a branch and then only passes forward the selected one since I haven’t implemented any kind of short circuit.

When I tried to implement lazy evaluation, I got a bit of a shock because not only did I get automatic short circuiting in any conditional branch but it also removed the need to allocate and deallocate transient constant nodes to store evaluated trees when doing strict evaluation. So it made the code simpler and the end was result was that evolution runs that were taking 4 days to run now take only 6 hours !

This was a lovely Christmas present, an order of magnitude efficiency increase that now makes more complex models possible to calculate. I never thought that learning Haskell would have such a direct positive influence on my C code.

Leave a Reply