]> source.charles.plessy.org Git - source/.git/commitdiff
Trois fois plus rapide en utilisant « skipMany » et en réduisant les appels à « try ».
authorCharles Plessy <https://launchpad.net/~plessy>
Tue, 23 Dec 2014 09:34:34 +0000 (18:34 +0900)
committerCharles Plessy <https://launchpad.net/~plessy>
Tue, 23 Dec 2014 09:34:52 +0000 (18:34 +0900)
Haskell/refSeqIdSymbol.html
Haskell/refSeqIdSymbol.lhs

index 8ea95200c5e5a22296ac53bd28eebed6b4b216be..8eb67b41ab419d270f73f08f32f732e17e78e52f 100644 (file)
@@ -65,8 +65,12 @@ parseGbRecord r <span class="fu">=</span> <span class="kw">case</span> parse gbR
     _          <span class="ot">-&gt;</span> endField <span class="fu">&gt;&gt;</span> return <span class="st">&quot;&quot;</span></code></pre>
 <p>A field starts with a field name, which is recorded. It is followed with multiple spaces. Since I am only intersted in version and gene symbol, if the field name was VERSION or FEATURES, more information is extracted, otherwise an empty string is returned.</p>
 <pre class="sourceCode literate haskell"><code class="sourceCode haskell">fieldName <span class="fu">=</span> many1 upper
-endField  <span class="fu">=</span> manyTill anyChar (try separator <span class="fu">&lt;|&gt;</span> try eof)
-separator <span class="fu">=</span> newline <span class="fu">&gt;&gt;</span> notFollowedBy (char <span class="ch">&#39; &#39;</span>)</code></pre>
+endField  <span class="fu">=</span> <span class="kw">do</span>
+  skipMany ((noneOf <span class="st">&quot;\n&quot;</span>) <span class="fu">&lt;|&gt;</span> try continuation)
+  separator
+  return <span class="st">&quot;&quot;</span>
+continuation <span class="fu">=</span> newline <span class="fu">&gt;&gt;</span> lookAhead     (char <span class="ch">&#39; &#39;</span>) <span class="fu">&gt;&gt;</span> return <span class="ch">&#39;\n&#39;</span>
+separator    <span class="fu">=</span> newline <span class="fu">&gt;&gt;</span> notFollowedBy (char <span class="ch">&#39; &#39;</span>) <span class="fu">&gt;&gt;</span> return <span class="ch">&#39;\n&#39;</span></code></pre>
 <p>A field name is upper case. Fields continue with any character until a separator or the end of the file is reached. A separator is a newline character not followed by a space.</p>
 <pre class="sourceCode literate haskell"><code class="sourceCode haskell">getVersionNumber <span class="fu">=</span> <span class="kw">do</span>
   <span class="kw">let</span> versionNumberChar <span class="fu">=</span> oneOf <span class="fu">$</span> <span class="st">&quot;NXRM_&quot;</span> <span class="fu">++</span> [<span class="ch">&#39;0&#39;</span><span class="fu">..</span><span class="ch">&#39;9&#39;</span>] <span class="fu">++</span> <span class="st">&quot;.&quot;</span>
@@ -122,9 +126,9 @@ NM_005355.3     KIF25
 NM_030615.2     KIF25
 
 real    0m0.036s
-user    0m0.028s
-sys     0m0.012s</code></pre>
-<p>The Haskell parser is unfortunately 100 times slower.</p>
+user    0m0.020s
+sys     0m0.020s</code></pre>
+<p>The Haskell parser is unfortunately 50 times slower.</p>
 <pre><code>$ time cat refSeqIdSymbol.testdata.gb | ./refSeqIdSymbol | head
 NM_025073.2     SIKE1
 NM_181712.4     KANK4
@@ -137,6 +141,6 @@ NM_182482.2     BAGE2
 NM_005355.3     KIF25
 NM_030615.2     KIF25
 
-real    0m4.963s
-user    0m4.808s
-sys     0m0.176s</code></pre>
+real    0m1.515s
+user    0m1.500s
+sys     0m0.036s</code></pre>
index abacdd65262c4f647844afbcb0b969676d18cb76..8537a010b85cbceb74d7a051d3dc9708551cf0a3 100644 (file)
@@ -137,8 +137,12 @@ field name was VERSION or FEATURES, more information is extracted, otherwise
 an empty string is returned.
 
 > fieldName = many1 upper
-> endField  = manyTill anyChar (try separator <|> try eof)
-> separator = newline >> notFollowedBy (char ' ')
+> endField  = do
+>   skipMany ((noneOf "\n") <|> try continuation)
+>   separator
+>   return ""
+> continuation = newline >> lookAhead     (char ' ') >> return '\n'
+> separator    = newline >> notFollowedBy (char ' ') >> return '\n'
 
 A field name is upper case.  Fields continue with any character until a
 separator or the end of the file is reached.  A separator is a newline
@@ -241,11 +245,11 @@ NM_005355.3     KIF25
 NM_030615.2     KIF25
 
 real    0m0.036s
-user    0m0.028s
-sys     0m0.012s
+user    0m0.020s
+sys     0m0.020s
 ~~~~~
 
-The Haskell parser is unfortunately 100 times slower.
+The Haskell parser is unfortunately 50 times slower.
 
 ~~~~~
 $ time cat refSeqIdSymbol.testdata.gb | ./refSeqIdSymbol | head
@@ -260,7 +264,7 @@ NM_182482.2     BAGE2
 NM_005355.3     KIF25
 NM_030615.2     KIF25
 
-real    0m4.963s
-user    0m4.808s
-sys     0m0.176s
+real    0m1.515s
+user    0m1.500s
+sys     0m0.036s
 ~~~~~