Evaluating a mathematical [removed]function) for a large number of input values fast

前端 未结 5 1623
你的背包
你的背包 2020-12-09 12:11

The following questions

  • Evaluating a mathematical expression in a string
  • Equation parsing in Python
  • Safe way to parse user-supplied
5条回答
  •  借酒劲吻你
    2020-12-09 12:17

    Since you asked about asteval, there is a way to use it and get faster results:

    aeval = Interpreter()
    time_start = time.time()
    expr = aeval.parse(userinput_function)
    for item in database_xy:
        aeval.symtable['x'] = item['x']
        item['y_aeval'] = aeval.run(expr)
    time_end = time.time()
    

    That is, you can first parse ("pre-compile") the user input function, and then insert each new value of x into the symbol table and the use Interpreter.run() to evaluate the compiled expression for that value. On your scale, I think this will get you close to 0.5 seconds.

    If you are willing to use numpy, a hybrid solution:

    aeval = Interpreter()
    time_start = time.time()
    expr = aeval.parse(userinput_function)
    x = numpy.array([item['x'] for item in database_xy])
    aeval.symtable['x'] = x
    y = aeval.run(expr)
    time_end = time.time()
    

    should be much faster, and comparable in run time to using numexpr.

提交回复
热议问题