[NumPy-Tickets] [NumPy] #2181: remove import-time 'exec' calls from polynomial

NumPy Trac numpy-tickets@scipy....
Fri Jul 6 08:39:27 CDT 2012

#2181: remove import-time 'exec' calls from polynomial
 Reporter:  dalke             |       Owner:  charris    
     Type:  defect            |      Status:  new        
 Priority:  normal            |   Milestone:  Unscheduled
Component:  numpy.polynomial  |     Version:  devel      
 Keywords:                    |  
 numpy/polynomial/*.py contains 5 files which do an 'exec' of a ~25K
 template, generated at import-time. The extra overhead of parsing and
 byte-compiling takes roughly 25% of the entire 'import numpy' overhead.

 I changed the code so it's the responsibility of the person who edits the
 template to rerun the 'polytemplate.py' file as a command-line program.
 This then goes through the correct files to find the special markers which
 indicate where the template should be inserted, get the file-specific
 parameters, apply those to the template, and replacing the old code which
 was between the two markers with the newly evaluated template output.

 In more details, currently there are 5 places under numpy/polynomial which
 do something like:

   exec polytemplate.substitute(name='Hermite', nick='herm',

 I have changed those so there are start/end markers, like

 #REPLACE POLYTEMPLATE name='Hermite', nick='herm', domain='[-1,1]'

 I have edited  polytemplate.py so that it implements a __main__ which
 finds those block markers and replaces the content with the appropriate
 "polytemplate" substitution.

 The performance of

     python -c 'import time; t1=time.time(); import numpy; print

 goes from 0.079 seconds to 0.057 seconds (best of 10), for an import
 speedup of ~25%.

Ticket URL: <http://projects.scipy.org/numpy/ticket/2181>
NumPy <http://projects.scipy.org/numpy>
My example project

More information about the NumPy-Tickets mailing list