[Haskell-cafe] newbie questions (read, etc., with Data.ByteString.Lazy.Char8)

wman 666wman at gmail.com
Tue Oct 7 02:34:05 EDT 2008


as usual, i forgot to use the magical "reply to all" and under the
impression I'm still talking to the list had bothered dons personally
(heresy/sacrilege/deathwish , i know)
to rectify it a bit at least, i'm posting a summary, in hopes someone find
it useful.
----------------------------
-- this doesn't work - space leak no matter which optimizations you use,
with -O2 it crashes in 5 secs instead in 75 ;-)
import qualified Data.ByteString.Lazy.Char8 as S
main = print . go 0 =<< S.getContents
  where
    go n s = case S.readInt s of
               Nothing     -> n
               Just (k,t)  -> go (n+k) (S.tail t)

-- slightly stricter version, notice those ! before parameters in go
declaration and the fancy -XBangPatterns option to ghc which it requires ;-)
-- let dons_bstr.hs =
import qualified Data.ByteString.Lazy.Char8 as S
main = print . go 0 =<< S.getContents
  where
    go !n !s = case S.readInt s of
               Nothing     -> n
               Just (k,t)  -> go (n+k) (S.tail t)

ghc -XBangPatterns --make dons_bstr.hs
time dons_bstr < nums
real    1m8.686s
user    0m0.015s
sys     0m0.031s

ghc -XBangPatterns -Onot -fstrictness --make dons_bstr.hs
time dons_bstr.exe < nums
real    1m10.607s
user    0m0.031s
sys     0m0.015s

ghc -XBangPatterns -O2 --make dons_bstr.hs
time dons_bstr.exe < nums
real    0m1.500s
user    0m0.015s
sys     0m0.031s

so althought the -fstrictness still seems to have no effect in this case,
using -O2 the dons version is ~2 times faster then the bytestring/readInt
version -O2
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.haskell.org/pipermail/haskell-cafe/attachments/20081007/243932a8/attachment.htm


More information about the Haskell-Cafe mailing list