[Haskell-cafe] Fusion for fun and profi (Was: newbie optimization question)

Don Stewart dons at galois.com
Sun Oct 28 16:43:07 EDT 2007


stefanor:
> 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?

It doesn't! The above code yields:

    Main.$wloop :: GHC.Prim.Int#
                   -> GHC.Prim.State# GHC.Prim.RealWorld
                   -> (# GHC.Prim.State# GHC.Prim.RealWorld, () #)

    $wgo_rMK :: GHC.Prim.Int# -> GHC.Prim.Int# -> GHC.Prim.Int# -> GHC.Prim.Int#
        where
            $s$wgo_rMI :: GHC.Prim.Int# -> GHC.Prim.Int# -> GHC.Prim.Int#


More information about the Haskell-Cafe mailing list