2

I'm writing a code in Python which can be used to solve equations. The user must first input a code via a raw_input(), which will then be used to calculate y for every x in a loop with eval(), like so:

#some imports (math) and other irrelevant code
Code = raw_input('please enter your equation')
Low = raw_input('please enter the lowest number in the domain')
High = raw_input('please enter the highest number in the domain')
X = Low
While X <= High:
    Y = eval(code)
    #complicated code to solve equation
    X += #number depending on the amount of decimals
#simpler code to print the result

The problem is that parsing the input code using eval() for every loop is extremely slow. Is there a way to parse the code only once and then use it as a function in the rest of the program?

2
  • Move it outside of the loop? Commented Feb 10, 2015 at 9:49
  • If "code" does not reference any variables/data that changes in the loop then you can just move that line to be above the while. Commented Feb 10, 2015 at 9:49

3 Answers 3

3

You can compile the expression in advance using compiler.compile() (deprecated since Python v2.6).

With Python 3.x:

Also take a look at: Python: Way to speed up a repeatedly executed eval statement?

EDIT

Some examples:

>>> expr = 'x*x + 2*x + 1'
>>> expr_obj = compile(expr, '', 'eval')
>>> x = 1
>>> y = eval(expr)

and now

>>> y
4
>>> x = 2
>>> y = eval(expr)
>>> y
9

compile is a lower level version of eval (and exec). It doesn't evaluate/execute your expression/statement but returns a code object that can do it.


PS as a general rule when using eval on user submitted strings you must be very careful about what you accept (it's a potential security hole).

Sign up to request clarification or add additional context in comments.

4 Comments

I might be doing something wrong, but compile(code) returns the location of the code instead of the evaluation.
compile doesn't evaluate your expression but returns a code object that can do it. I've added an example.
Thank you, that reduced calculation times significantly!
To give you an example: it can now calculate all 128 intersections of tan(x**2)=y with the x-axis in just 3300 miliseconds accurate to 7 decimals on my 1.4Ghz iPad.
1

if the equation in code doen't need any reference to X, then you could move Y = eval(code) outside the loop. Otherwise you need it at this position to access X and its current value.

1 Comment

If the equation doesn't reference X, then the value of Y would be constant, and there would be no need for the loop in the first place. It looks like the OP is trying to get a table of values of Y for different values of X, implying that Y is a function of X.
0

If the user inputs the equation in terms of a single variable "X", then you can convert it to a function using Python's lambda keyword:

CodeAsFunction = eval('lambda X : ' + Code)

(For this to work, the user just enters the right-hand side of the equation, 3*X + 100, not Y = 3*X+100.)

Then in the body of your loop, you can call this function instead:

while X = High:
    Y = CodeAsFunction(X)
    ... etc. ...

Note, eval is now only being called once, to create the callable CodeAsFunction variable.

Of course, you must take care regarding security issues, as have already been mentioned by other posters.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.