[Haskell-cafe] strict bytestring fun

Donald Bruce Stewart dons at cse.unsw.edu.au
Tue Feb 6 03:22:29 EST 2007


bertram.felgenhauer:
> Donald Bruce Stewart wrote:
> > High performance strings on the shootout:
> > 
> > http://shootout.alioth.debian.org/gp4/benchmark.php?test=sumcol&lang=all
> > 
> > interesting alternative programs
> > 0.5   Haskell GHC #5          1.29            90,880        270
> > 
> > 1.0   Clean                   2.77            600           136
> [snip]
> 
> > Ah well, its illegally strict, but its good to know you can do it, eh?
> > 
> > (A lazy bytestring version has been submitted, we'll see how that runs).
> 
> Hmm, apparently it's a factor of 10 slower. Using unsafeHead and
> unsafeTail in the strict bytestring version is a *very* big win.
> Without that it'd be a more reasonable factor of about 2 between the
> lazy and the strict bytestring versions, according to my own tests.
> 
> FWIW, the more natural
> 
> > {-# OPTIONS -O -fbang-patterns #-}
> > import qualified Data.ByteString.Lazy.Char8 as B
> > 
> > main = print . loop 0 =<< B.getContents
> > 
> > loop !s !xs = case B.readInt xs of
> >    Just (n, xs') -> loop (s+n) (B.tail xs') -- drop the newline
> >    Nothing       -> s
> 
> runs slightly faster than the explicit parsing done by the current
> lazy bytestring entry in my tests.

Ah, I didn't try that variant. Feel free to submit it if its faster
than:
    http://shootout.alioth.debian.org/gp4/benchmark.php?test=sumcol&lang=ghc&id=3

    import Data.List
    import qualified Data.ByteString.Lazy.Char8 as L

    main = print . foldl' (+) 0 . unfoldr parse =<< L.getContents

    parse !s | Just (n,t) <- L.readInt s = Just (n, L.tail t)
             | otherwise                 = Nothing


I've updated most of the other entries now, except regex-dna, nbody and
spectral-norm. Things are looking quite good.

-- Don


More information about the Haskell-Cafe mailing list