On 09/03/2013 06:09 PM, Dan Burton wrote:
Here's a fun alternative for you to benchmark, using an old trick. I kind of
doubt that this one will optimize as nicely as the others, but I am by no means
an optimization guru:
allPairsS :: [a] - [(a, a)]
allPairsS xs = go xs [] where
go [] = id
Awesome/ thanks for sharing. It's worth noting that criterion is also
pretty great for microbenchmarks too. On my machine I get pretty good
timing accuracy on anything that takes more than 20 nanoseconds.
On Wednesday, September 4, 2013, Scott Pakin wrote:
On 09/03/2013 06:02 PM, Carter
On 09/03/2013 06:02 PM, Carter Schonwald wrote:
It's also worth adding that ghci does a lot less optimization than ghc.
Yes, I discovered that before I posted. Note from my initial message
that I used ghc to compile, then loaded the compiled module into ghci:
Prelude :!ghc -c -O2
On 09/03/2013 05:43 PM, Bob Ippolito wrote:
Haskell's non-strict evaluation can often lead to unexpected results when doing
tail recursion if you're used to strict functional programming languages. In
order to get the desired behavior you will need to force the accumulator (with
something
I'm a Haskell beginner, and I'm baffled trying to reason about code
performance, at least with GHC. For a program I'm writing I needed to
find all pairs of elements of a list. That is, given the list ABCD
I wanted to wind up with the list
Haskell's non-strict evaluation can often lead to unexpected results when
doing tail recursion if you're used to strict functional programming
languages. In order to get the desired behavior you will need to force the
accumulator (with something like Data.List's foldl', $!,
seq, BangPatterns,
It's also worth adding that ghci does a lot less optimization than ghc.
Likewise, the best tool for doing performance benchmarking is the excellent
Criterion library.
To repeat: use compiled code for benchmarking, and use criterion. Ghci is
not as performance tuned as compiled code, except when
Well for one thing, note that allPairs3 produces the result in reverse
order:
allPairs1 abc
[('a','b'),('a','c'),('b','c')]
allPairs2 abc
[('a','b'),('a','c'),('b','c')]
allPairs3 abc
[('b','c'),('a','c'),('a','b')]
allPairs2 uses guarded recursion which the optimizer probably likes,
although
allPairs2 can be simplified using a trick I wouldn't dare use in
any language but Haskell:
triangle4 xs = fused undefined [] xs
where fused x (y:ys) zs = (x,y) : fused x ys zs
fused _ [] (z:zs) = fused z zs zs
fused _ [] [] = []
I submit this just for grins; it