7 Oct 08:34
Re: newbie questions (read, etc., with Data.ByteString.Lazy.Char8)
From: wman <666wman <at> gmail.com>
Subject: Re: newbie questions (read, etc., with Data.ByteString.Lazy.Char8)
Newsgroups: gmane.comp.lang.haskell.cafe
Date: 2008-10-07 06:34:05 GMT
Subject: Re: newbie questions (read, etc., with Data.ByteString.Lazy.Char8)
Newsgroups: gmane.comp.lang.haskell.cafe
Date: 2008-10-07 06:34:05 GMT
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 =
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
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)
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
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe <at> haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
RSS Feed