Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>So I did some experimenting and I got a wc -l that is only about twice as slow as "wc -l" This is better performance than even the wc -c version shown above.</p> <pre><code>{-# LANGUAGE OverloadedStrings #-} import qualified Data.ByteString.Lazy.Char8 as BSL import qualified Data.ByteString.Char8 as BS import qualified Data.Enumerator as E import qualified Data.Enumerator.Binary as EB import Control.Monad.IO.Class (liftIO) import Data.Int numlines :: Int64 -&gt; E.Iteratee BS.ByteString IO () numlines n = do chunk &lt;- EB.take 1024 case chunk of "" -&gt; do liftIO $ print n return () a -&gt; do let ct = BSL.count '\n' a numlines (n+ct) main = do let i = EB.enumFile "/usr/share/dict/words" E.$$ numlines 0 E.run_ i </code></pre> <p>Running it vs. native:</p> <pre><code>Eriks-MacBook-Air:skunk erikhinton$ time wc -l "/usr/share/dict/words" 235886 /usr/share/dict/words real 0m0.009s user 0m0.006s sys 0m0.002s Eriks-MacBook-Air:skunk erikhinton$ time ./wcl 235886 real 0m0.019s user 0m0.013s sys 0m0.005s </code></pre> <p>[EDIT]</p> <p>Here's an even faster, smaller footprint and far more concise/expressive way of doing it. These enumerators are starting to get fun.</p> <pre><code>{-# LANGUAGE OverloadedStrings #-} import qualified Data.ByteString.Lazy.Char8 as BSL import qualified Data.ByteString.Char8 as BS import qualified Data.Enumerator as E import qualified Data.Enumerator.Binary as EB import qualified Data.Enumerator.List as EL import Control.Monad.IO.Class (liftIO) import Data.Int numlines :: E.Iteratee BS.ByteString IO () numlines = do num &lt;- EL.fold (\n b -&gt; (BS.count '\n' b) + n ) 0 liftIO . print $ num main = do let i = EB.enumFile "/usr/share/dict/words" E.$$ numlines E.run_ i </code></pre> <p>And the timing</p> <pre><code>Eriks-MacBook-Air:skunk erikhinton$ time ./wcl2 235886 real 0m0.015s user 0m0.010s sys 0m0.004s </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload