<div dir="ltr"><div><div><div>Hi Alex <br></div>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]. <br></div>Try this one. <br>import qualified Data.ByteString.Lazy.Char8 as BS<br>
import Data.Maybe ( fromJust )<br>import Data.List <br><br>readI :: BS.ByteString -> Integer<br>readI = fst . fromJust . BS.readInteger <br><br>main = BS.interact $ sumFile where<br> sumFile = BS.pack . show . sum' . map readI . BS.words <br>
sum' = foldl' (+) 0<br><br></div>-Mukesh<br><div><div><br>[1] <a href="http://www.haskell.org/haskellwiki/Memory_leak">http://www.haskell.org/haskellwiki/Memory_leak</a><br></div></div></div><div class="gmail_extra">
<br><br><div class="gmail_quote">On Sat, Mar 23, 2013 at 8:59 PM, Axel Wegen <span dir="ltr"><<a href="mailto:axel.wegen@gmail.com" target="_blank">axel.wegen@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<div class="im">mukesh tiwari <<a href="mailto:mukeshtiwari.iiitm@gmail.com">mukeshtiwari.iiitm@gmail.com</a>> writes:<br>
> It's already mentioned there "A String is represented as a list of<br>
> Char values; each element of a list is allocated individually, and has<br>
> some book-keeping overhead. These factors affect the memory<br>
> consumption and performance of a program that must read or write text<br>
> or binary data. On simple benchmarks like this, even programs written<br>
> in interpreted languages such as Python can outperform Haskell code<br>
> that uses String by an order of magnitude".<br>
<br>
</div>I assumed that just means using plain Strings for that job will take<br>
more time.<br>
<div class="im"><br>
> import qualified Data.ByteString.Lazy.Char8 as BS<br>
> import Data.Maybe ( fromJust )<br>
><br>
> readI :: BS.ByteString -> Integer<br>
> readI = fst . fromJust . BS.readInteger<br>
><br>
> main = BS.interact sumFile where<br>
> sumFile = BS.pack . show . sum . map readI . BS.words<br>
<br>
</div>I get the same result, the stack overflow. Though I don't have wait as<br>
long for it to break.<br>
<br>
I think that there is something about the summation that makes it<br>
impossible for the compiler to do it's magic and optimize the thing to<br>
something less stack overflowing. I just don't understand what that is.<br>
<br>
--<br>
Axel Wegen<br>
<div class="HOEnZb"><div class="h5"><br>
_______________________________________________<br>
Beginners mailing list<br>
<a href="mailto:Beginners@haskell.org">Beginners@haskell.org</a><br>
<a href="http://www.haskell.org/mailman/listinfo/beginners" target="_blank">http://www.haskell.org/mailman/listinfo/beginners</a><br>
</div></div></blockquote></div><br></div>