15 May 20:29
Write Haskell as fast as C. [Was: Re: GHC predictability]
From: Don Stewart <dons <at> galois.com>
Subject: Write Haskell as fast as C. [Was: Re: GHC predictability]
Newsgroups: gmane.comp.lang.haskell.cafe
Date: 2008-05-15 18:31:32 GMT
Subject: Write Haskell as fast as C. [Was: Re: GHC predictability]
Newsgroups: gmane.comp.lang.haskell.cafe
Date: 2008-05-15 18:31:32 GMT
> I offer up the following example: > > mean xs = sum xs / length xs > > Now try, say, "mean [1.. 1e9]", and watch GHC eat several GB of RAM. (!!) > > If we now rearrange this to > > mean = (\(s,n) -> s / n) . foldr (\x (s,n) -> let s' = s+x; n' = n+1 > in s' `seq` n' `seq` (s', n')) (0,0) > > and run the same example, and watch it run in constant space. > > Of course, the first version is clearly readable, while the second one > is almost utterly incomprehensible, especially to a beginner. (It's even > more fun that you need all those seq calls in there to make it work > properly.) > > The sad fact is that if you just write something in Haskell in a nice, > declarative style, then roughly 20% of the time you get good > performance, and 80% of the time you get laughably poor performance. I've written an extended post on how to understand and reliably optimise code like this, looking at it all the way down to the assembly. The result are some simple rules to follow for generated code as good as gcc -O2. Enjoy,(Continue reading)
> Side point: Is the name "go" part of the idiom you mentioned? I
> sometimes use the same practise but usually just calls the worker the
> same as the real function with an added prime (').
>
I usually use "work". Same difference.
From what I can tell, the Prelude implementation in the Haskell Report
uses primes on the function names [in the tiny number of cases where a
worker function is actually required]. Each to their own...
RSS Feed