On Tue, Feb 13, 2007 at 08:18:25AM +1100, Donald Bruce Stewart wrote: > Now, if we rewrite it to not use the temporary: > > go :: Double -> Double -> Int -> IO () > go !x !y !i > | i == 1000000000 = printf "%.6f\n" (x+y) > | otherwise = go (x*y/3) (x*9) (i+1) > > > for (; i<1000000000; i++) { > x = x*y/3.0; > y = x*9.0; > } > > ------------------------------------------------------------------------ > > $ time ./hp > 3.333333 > ./hp 9.95s user 0.00s system 99% cpu 9.965 total > > $ time ./cc > 3.333333 > ./cc 10.06s user 0.00s system 99% cpu 10.110 total
I'm rather curious (if you're sill interested) how this'll be affected by the removal of the division from the inner loop. e.g. go :: Double -> Double -> Int -> IO () go !x !y !i | i == 1000000000 = printf "%.6f\n" (x+y) | otherwise = go (x*y*(1.0/3)) (x*9) (i+1) for (; i<1000000000; i++) { x = x*y*(1.0/3.0); y = x*9.0; } My guess is that the code will be far faster, and that the differences between C and Haskell will therefore be more pronounced. After all, division is a slow operation... -- David Roundy Department of Physics Oregon State University _______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe