Thread: Speeding up parsed equations

  1. #1
    Registered User
    Join Date
    Jun 2008

    Speeding up parsed equations

    Hi, I have a problem with the speed of parsed equations. I want the user to be able to define a equation X.

    Lets say they define it as X=y+1

    then I execute
    while (i < MAX)
    Since it's user defined I call a parser to intepret the equation and return the value.
    Unfortunately since it's calculated a million times everything gets seriously bogged down reparsing it every cycle. Is there a way to get it up to at least close to the speeds it would be if X was hardcoded? I've gotten suggestions like gnu lightning and writing my own virtual machine but they all are extremely complex, using often underdeveloped resources and often require the use of assembly. And I'm pretty much still a novice.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Do you call the parser every time through the loop?

    Could you store the result of the parse (maybe using an expression tree) each time instead?

  3. #3
    Join Date
    Mar 2008
    i did something like that with PHP.

    i had the user declare what the variables were, enter the equation, and then i parsed the input, editing the string to make it compile, and then i used the php command line interface to run the code and i got the result from stdin.

    it was pretty simple to implement and it was reasonably fast.

    i'm sure you could build some kind of shell script and accomplish the same thing.

  4. #4
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Portland, OR
    Feh. A Real Programmer would write code to translate the equation to C code, write it to a file, call the compiler on it, turn it into a DLL, then dynamically load it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Read coefficients of linear equations from file into 2d array
    By omaralqady in forum C++ Programming
    Replies: 6
    Last Post: 06-20-2009, 07:39 AM
  2. Rendering math equations
    By glo in forum Game Programming
    Replies: 7
    Last Post: 02-01-2009, 04:21 PM
  3. LUP Decomposition (simultaneous equations)
    By Markallen85 in forum C Programming
    Replies: 6
    Last Post: 08-24-2003, 02:08 AM
  4. Manipulation and analysis of Mathematical Equations
    By WebmasterMattD in forum C++ Programming
    Replies: 13
    Last Post: 06-07-2003, 10:08 PM
  5. equations for a program
    By anthonye in forum C Programming
    Replies: 4
    Last Post: 06-19-2002, 04:38 AM