Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I've come up with a Haskell solution that is 30× faster than the Haskell solution you posted (with my concocted test expression).</p> <p>Major changes:</p> <ol> <li>Change Parsec/String to Attoparsec/ByteString</li> <li>In the <code>fact</code> function, change <code>read</code> &amp; <code>many1 digit</code> to <code>decimal</code></li> <li>Made the <code>chainl1</code> recursion strict (remove $! for the lazier version).</li> </ol> <p>I tried to keep everything else you had as similar as possible.</p> <pre><code>import Control.Applicative import Data.Attoparsec import Data.Attoparsec.Char8 import qualified Data.ByteString.Char8 as B expr :: Parser Int expr = chainl1 term ((+) &lt;$ char '+' &lt;|&gt; (-) &lt;$ char '-') term :: Parser Int term = chainl1 fact ((*) &lt;$ char '*' &lt;|&gt; div &lt;$ char '/') fact :: Parser Int fact = decimal &lt;|&gt; char '(' *&gt; expr &lt;* char ')' eval :: B.ByteString -&gt; Int eval = either (error . show) id . eitherResult . parse expr . B.filter (/= ' ') chainl1 :: (Monad f, Alternative f) =&gt; f a -&gt; f (a -&gt; a -&gt; a) -&gt; f a chainl1 p op = p &gt;&gt;= rest where rest x = do f &lt;- op y &lt;- p rest $! (f x y) &lt;|&gt; pure x main :: IO () main = B.readFile "expr" &gt;&gt;= (print . eval) </code></pre> <p>I guess what I concluded from this is that the majority of the slowdown for the parser combinator was that it was sitting on an inefficient base, not that it was a parser combinator, per se.</p> <p>I imagine with more time and profiling this could go faster, as I stopped when I went past the 25× mark.</p> <p>I don't know if this would be faster than the precedence climbing parser ported to Haskell. Maybe that would be an interesting test?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

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