Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You can speed it up by using <code>ByteString</code>s, e.g.</p> <pre><code>module Main (main) where import System.Environment (getArgs) import qualified Data.ByteString.Lazy.Char8 as C import Data.Char main :: IO () main = do args &lt;- getArgs mapM_ doFile args doFile :: FilePath -&gt; IO () doFile file = do bs &lt;- C.readFile file let tups = buildTups 0 [] $ C.dropWhile (not . isDigit) bs print (length tups) buildTups :: Int -&gt; [Int] -&gt; C.ByteString -&gt; [(Int,Int,Int,Int,Int,Int)] buildTups 6 acc bs = tuplify6 acc : buildTups 0 [] bs buildTups k acc bs | C.null bs = if k == 0 then [] else error ("Bad file format " ++ show k) | otherwise = case C.readInt bs of Just (i,rm) -&gt; buildTups (k+1) (i:acc) $ C.dropWhile (not . isDigit) rm Nothing -&gt; error ("No Int found: " ++ show (C.take 100 bs)) tuplify6:: [a] -&gt; (a, a, a, a, a, a) tuplify6 [l, m, n, o, p, q] = (l, m, n, o, p, q) </code></pre> <p>runs pretty fast:</p> <pre><code>$ time ./fileParse IntList 200000 real 0m0.119s user 0m0.115s sys 0m0.003s </code></pre> <p>for an 8.1 MiB file.</p> <p><del>On the other hand, using <code>String</code>s and your conversion (with a couple of <code>seq</code>s to force evaluation) also took only 0.66s, so the bulk of the time seems to be spent not parsing, but working with the result.</del></p> <p>Oops, missed a <code>seq</code> so the <code>read</code>s were not actually evaluated for the <code>String</code> version. Fixing that, <code>String</code> + <code>read</code> takes about four seconds, a bit above one with the custom <code>Int</code> parser from @Rotsor's comment</p> <pre><code>foldl' (\a c -&gt; 10*a + fromEnum c - fromEnum '0') 0 </code></pre> <p>so parsing apparently did take a significant amount of the time.</p>
 

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