[Haskell-cafe] Monte Carlo Pi calculation (newbie learnings)
Brandon S. Allbery KF8NH
allbery at ece.cmu.edu
Mon Nov 5 23:55:31 EST 2007
On Nov 5, 2007, at 16:21 , Alex Young wrote:
> C:\Users\Alex\Documents\HaskellLearning\MonteCarlo>ghc BetterPi.hs
>
> C:\Users\Alex\Documents\HaskellLearning\MonteCarlo>main.exe 1000000
> Stack space overflow: current size 8388608 bytes.
> Use `+RTS -Ksize' to increase it.
>
> But:
>
> C:\Users\Alex\Documents\HaskellLearning\MonteCarlo>ghc -O2 BetterPi.hs
>
> C:\Users\Alex\Documents\HaskellLearning\MonteCarlo>main.exe 1000000
> 3.140636
>
> This is a little confusing. Is there a simple explanation for this
> behaviour, or is it just a matter of "always use -O2 unless there's a
> reason not to?"
Basically, one of the optimizations enabled by -O2 causes ghc to
notice that it doesn't need to collect a bunch of thunks on the
stack, but instead can use them as they're generated. ("fusion" ---
in this case probably some build/fold fusion)
I would indeed say that in most cases you want to use -O2 just to get
the smarter behavior, unless you're trying to learn how to write
efficient code to start with; but on the other hand, it's nice to be
able to write *readable* code and have the compiler figure out how to
make it efficient. (Much the same goes for C, by the way; I can
write code to the bare metal, or write comprehensible code and let cc
work out how to make it fast. Unless I (a) absolutely need the
performance and (b) know the optimizer's not smart enough to do it
for me, I'll go for readable instead of e.g. Duff's Device.)
--
brandon s. allbery [solaris,freebsd,perl,pugs,haskell] allbery at kf8nh.com
system administrator [openafs,heimdal,too many hats] allbery at ece.cmu.edu
electrical and computer engineering, carnegie mellon university KF8NH
More information about the Haskell-Cafe
mailing list