[Haskell-beginners] stack overflow summing numbers read from a big file

mukesh tiwari mukeshtiwari.iiitm at gmail.com
Sat Mar 23 15:38:32 CET 2013


Hi Alex
I am no expert but try to profile your code and see the memory usage. It
seems like the  sum function is causing the stack overflow[1].
Try this one.
import qualified Data.ByteString.Lazy.Char8 as BS
import Data.Maybe ( fromJust )
import Data.List

readI :: BS.ByteString -> Integer
readI = fst . fromJust . BS.readInteger

main = BS.interact $ sumFile   where
     sumFile =  BS.pack . show . sum' . map readI . BS.words
     sum' = foldl' (+) 0

-Mukesh

[1] http://www.haskell.org/haskellwiki/Memory_leak


On Sat, Mar 23, 2013 at 8:59 PM, Axel Wegen <axel.wegen at gmail.com> wrote:

> mukesh tiwari <mukeshtiwari.iiitm at gmail.com> writes:
> > It's already mentioned there "A String is represented as a list of
> > Char values; each element of a list is allocated individually, and has
> > some book-keeping overhead. These factors affect the memory
> > consumption and performance of a program that must read or write text
> > or binary data. On simple benchmarks like this, even programs written
> > in interpreted languages such as Python can outperform Haskell code
> > that uses String by an order of magnitude".
>
> I assumed that just means using plain Strings for that job will take
> more time.
>
> > import qualified Data.ByteString.Lazy.Char8 as BS
> > import Data.Maybe ( fromJust )
> >
> > readI :: BS.ByteString -> Integer
> > readI = fst . fromJust . BS.readInteger
> >
> > main = BS.interact sumFile where
> > sumFile = BS.pack . show . sum . map readI . BS.words
>
> I get the same result, the stack overflow. Though I don't have wait as
> long for it to break.
>
> I think that there is something about the summation that makes it
> impossible for the compiler to do it's magic and optimize the thing to
> something less stack overflowing. I just don't understand what that is.
>
> --
> Axel Wegen
>
> _______________________________________________
> Beginners mailing list
> Beginners at haskell.org
> http://www.haskell.org/mailman/listinfo/beginners
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/beginners/attachments/20130323/702d8eb9/attachment.htm>


More information about the Beginners mailing list