[Haskell-cafe] strict bytestring fun

Bertram Felgenhauer bertram.felgenhauer at googlemail.com
Fri Feb 2 13:03:39 EST 2007


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.

Bertram


More information about the Haskell-Cafe mailing list