<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&#39;s actually _slower_ than the non-bytestring version.<br>
<br>i did a little test, three versions of the same script and manufactured meself&nbsp; a ~50 MB file containing 1M of&nbsp; 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>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; contents &lt;- L.getContents <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; print (sumFile contents)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; where sumFile = length . map L.readInt . L.words<br><br>main2 = do<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; contents &lt;- getContents <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; print (sumFile contents)<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; where sumFile = length . map (read :: String -&gt; Int) . words<br><br>main3 = do<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; contents &lt;- L.getContents <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; print (sumFile contents)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; where sumFile = length . map ((read :: String -&gt; Int) . L.unpack) . L.words&nbsp; <br>
<br>time main3 &lt; nums<br>real&nbsp;&nbsp;&nbsp; 0m22.421s<br>user&nbsp;&nbsp;&nbsp; 0m0.031s<br>sys&nbsp;&nbsp;&nbsp;&nbsp; 0m0.000s<br><br> time main2 &lt; nums<br>real&nbsp;&nbsp;&nbsp; 0m14.296s<br>user&nbsp;&nbsp;&nbsp; 0m0.015s<br>sys&nbsp;&nbsp;&nbsp;&nbsp; 0m0.016s<br><br> time main1 &lt; nums<br>real&nbsp;&nbsp;&nbsp; 0m22.078s<br>
user&nbsp;&nbsp;&nbsp; 0m0.015s<br>sys&nbsp;&nbsp;&nbsp;&nbsp; 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&nbsp; ...<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">&lt;<a href="mailto:tutufan@gmail.com">tutufan@gmail.com</a>&gt;</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. &nbsp;I was looking through the Real World<br>
Haskell book and came across a trivial program for summing numbers in<br>
a file. &nbsp;They mentioned that that implementation was very slow, as<br>
it&#39;s based on String&#39;s, so I thought I&#39;d try my hand at converting it<br>
to use lazy ByteString&#39;s. &nbsp;I&#39;ve made some progress, but now I&#39;m a<br>
little stuck because that module doesn&#39;t seem to have a &#39;read&#39; method.<br>
<br>
There&#39;s a readInt method, which I guess I could use, but it returns a<br>
Maybe, and I don&#39;t see how I can easily strip that off.<br>
<br>
So:<br>
<br>
1. &nbsp;Is there an easy way to strip off the Maybe that would allow an<br>
equivalently concise definition for sumFile? &nbsp;I can probably figure<br>
out how to do it with pattern matching and a separate function--I&#39;m<br>
just wondering if there&#39;s a more concise way.<br>
<br>
2. &nbsp;Why doesn&#39;t ByteString implement &#39;read&#39;? &nbsp;Is it just that this<br>
function (like &#39;input&#39; in Python) isn&#39;t really very useful for real<br>
programs?<br>
<br>
3. &nbsp;Why doesn&#39;t ByteString implement &#39;readDouble&#39;, etc.? &nbsp;That is, why<br>
are Int and Integer treated specially? &nbsp;Do I not need readDouble?<br>
<br>
Thanks,<br>
Mike<br>
<br>
<br>
-- lazy version (doesn&#39;t compile)<br>
<br>
-- file: ch08/SumFile.hs<br>
<br>
import qualified Data.ByteString.Lazy.Char8 as L<br>
<br>
main = do<br>
 &nbsp; &nbsp; &nbsp; &nbsp; contents &lt;- L.getContents<br>
 &nbsp; &nbsp; &nbsp; &nbsp; print (sumFile contents)<br>
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 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>