Hmm... I think I made a little confusion so I put my finding here:<div><a href="http://haisgwu.info/posts/2011-11-20-euler-problem-14.html">http://haisgwu.info/posts/2011-11-20-euler-problem-14.html</a></div><div><br></div>
<div>I do got stack overflow thus need several compile opts to fix it.</div><div>Not sure if it is what you mean by "You get overflow using 32-bit types here."<br><div><br clear="all">-Haisheng<br>
<br><br><div class="gmail_quote">On Sat, Nov 19, 2011 at 10:49 PM, Daniel Fischer <span dir="ltr"><<a href="mailto:daniel.is.fischer@googlemail.com">daniel.is.fischer@googlemail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">
<div class="im">On Saturday 19 November 2011, 09:09:50, Haisheng Wu wrote:<br>
> Hello,<br>
> I got great performance difference for the following code if I used<br>
> type `Int` rather than `Data.Word.Word32`.<br>
> Anyone can help to explain why such difference?<br>
<br>
</div>Short answer: GHC optimises Int calculations far better than Word32<br>
calculations, rather, it optimises more calculations for Int than for<br>
Word32. It also optimises more calculations for Word than for Word32, but<br>
not as many as for Int.<br>
<br>
The reason is that Int (and Word) are (at least expected to be) far more<br>
often used, so the effort has gone to these types primarily. Work is being<br>
done to get the fixed-width types on par, but it's not around the corner<br>
yet.<br>
<br>
You can try using Word instead of Word32, that's likely to be faster.<br>
<br>
But<br>
<div class="im"><br>
> Thanks a lot.<br>
><br>
> -Simon<br>
><br>
> module Main where<br>
> import Data.Word<br>
><br>
> main :: IO ()<br>
> main = print $ p14<br>
><br>
> p14 = maximum [ (startChain n 0, n) | n <- [2..1000000] ]<br>
<br>
</div>You get overflow using 32-bit types here.<br>
<div class="HOEnZb"><div class="h5"><br>
><br>
> startChain :: Word32 -> Int -> Int<br>
> startChain 1 count = count + 1<br>
> startChain n count = startChain (intTransform n) (count+1)<br>
><br>
> intTransform :: Word32 -> Word32<br>
> intTransform n<br>
><br>
> | even n = n `div` 2<br>
> | otherwise = 3 * n + 1<br>
<br>
</div></div></blockquote></div><br></div></div>