math - python: code optimisation by precalculation of constant therms -
i write flexible simulation program. flexibe because user should able customize calculations via python code. customisation begins defining "constants" (constants in sense can never change value). can use constants , few known predefined variables define python calculation code. code later processed in fixed program code. here simplified example:
# begin user input width = 20.0 # user defined constants cond = 10.0 calc_code = "cond*width*x" # x known changing variable # end user input # begin hidden fixed program code code = compile( ''' def calc(x): # function executes user code return ''' + calc_code , '<string>', 'exec') exec(code) # python 2.x remove brackets x in range(10): print(calc(x)) # python 2.x remove brackets # end hidden fixed program code
the problem user code executed , slows down calculation significantly. possible somehow automaticaly find , precalculate constant terms in code (cond*width) significant speedup?
Comments
Post a Comment