
On Sun, Oct 28, 2007 at 01:25:19PM -0700, Don Stewart wrote:
Finally, we can manually translate the C code into a confusing set of nested loops with interleaved IO,
main = loop 1 where loop !i | i > 10000 = return () | otherwise = if i == go i 0 1 then print i >> loop (i+1) else loop (i+1)
go !i !s !j | j <= i-1 = if i `rem` j == 0 then go i (s+j) (j+1) else go i s (j+1) | otherwise = s
And we get *no speed benefit* at all!
time ./A-loop 1.24s user 0.00s system 98% cpu 1.256 total
So the lesson is: write in a high level style, and the compiler can do the work for you. Or, GHC is pretty smart on high level code.
IO blocks unboxing in GHC. How fast is your mock-C code refactored to do IO outside of the loops only? Stefan