<div dir="ltr">a slight modification to compile it : <br><br>change:<br>where sumFile = sum . map read . L.words<br>to :<br>where sumFile = sum . map (read . L.unpack) . L.words<br><br>but it's actually _slower_ than the non-bytestring version.<br>
<br>i did a little test, three versions of the same script and manufactured meself a ~50 MB file containing 1M of ints 0-65535. and replaced the sum with length for obvious reasons.<br><br>module Main where<br>import qualified Data.ByteString.Lazy.Char8 as L<br>
<br>main1 = do <br> contents <- L.getContents <br> print (sumFile contents)<br> where sumFile = length . map L.readInt . L.words<br><br>main2 = do<br> contents <- getContents <br> print (sumFile contents)<br>
where sumFile = length . map (read :: String -> Int) . words<br><br>main3 = do<br> contents <- L.getContents <br> print (sumFile contents)<br> where sumFile = length . map ((read :: String -> Int) . L.unpack) . L.words <br>
<br>time main3 < nums<br>real 0m22.421s<br>user 0m0.031s<br>sys 0m0.000s<br><br> time main2 < nums<br>real 0m14.296s<br>user 0m0.015s<br>sys 0m0.016s<br><br> time main1 < nums<br>real 0m22.078s<br>
user 0m0.015s<br>sys 0m0.015s<br><br>i expected the conversions (L.unpack in main3) to kill the performance a little, but not to make it nearly two times as slow.<br>and i certainly did not expect that even the version using the bytestring readInt to be as slow ...<br>
<br>did I do something wrong ?<br><br><br><div class="gmail_quote">On Tue, Oct 7, 2008 at 4:06 AM, Mike Coleman <span dir="ltr"><<a href="mailto:tutufan@gmail.com">tutufan@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
Hi,<br>
<br>
I could use a little help. I was looking through the Real World<br>
Haskell book and came across a trivial program for summing numbers in<br>
a file. They mentioned that that implementation was very slow, as<br>
it's based on String's, so I thought I'd try my hand at converting it<br>
to use lazy ByteString's. I've made some progress, but now I'm a<br>
little stuck because that module doesn't seem to have a 'read' method.<br>
<br>
There's a readInt method, which I guess I could use, but it returns a<br>
Maybe, and I don't see how I can easily strip that off.<br>
<br>
So:<br>
<br>
1. Is there an easy way to strip off the Maybe that would allow an<br>
equivalently concise definition for sumFile? I can probably figure<br>
out how to do it with pattern matching and a separate function--I'm<br>
just wondering if there's a more concise way.<br>
<br>
2. Why doesn't ByteString implement 'read'? Is it just that this<br>
function (like 'input' in Python) isn't really very useful for real<br>
programs?<br>
<br>
3. Why doesn't ByteString implement 'readDouble', etc.? That is, why<br>
are Int and Integer treated specially? Do I not need readDouble?<br>
<br>
Thanks,<br>
Mike<br>
<br>
<br>
-- lazy version (doesn't compile)<br>
<br>
-- file: ch08/SumFile.hs<br>
<br>
import qualified Data.ByteString.Lazy.Char8 as L<br>
<br>
main = do<br>
contents <- L.getContents<br>
print (sumFile contents)<br>
where sumFile = sum . map read . L.words<br>
_______________________________________________<br>
Haskell-Cafe mailing list<br>
<a href="mailto:Haskell-Cafe@haskell.org">Haskell-Cafe@haskell.org</a><br>
<a href="http://www.haskell.org/mailman/listinfo/haskell-cafe" target="_blank">http://www.haskell.org/mailman/listinfo/haskell-cafe</a><br>
</blockquote></div><br></div>