In [1]:
import jittermodel
from jittermodel.base import Cantilever, Transistor, Experiment
from jittermodel.plot import GeneratePlotData
from jittermodel import u

import cProfile
import pstats

def make_plot():
    c1 = Cantilever()
    t1 = Transistor(h=70*u.nm)
    e1 = Experiment()
    gpd = GeneratePlotData(c1, t1, e1, 'd', (40*u.nm, 500*u.nm))
    V_g_vals = (1*u.V, 20*u.V, 40*u.V)
    gpd.calc_plot_data('jitter', 'd', 'V_g', V_g_vals,
                       x_scale='log', n_pts=5)

In [2]:
def my_profile():
    cProfile.run('make_plot()', 'unit_stats')
    p = pstats.Stats('unit_stats')
    p.strip_dirs()
    p.sort_stats('time').print_stats(10)

In [3]:
my_profile()


---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-3-8736898e33e4> in <module>()
----> 1 my_profile()

<ipython-input-2-5fe3a0ad9f6c> in my_profile()
      1 def my_profile():
----> 2     cProfile.run('make_plot()', 'unit_stats')
      3     p = pstats.Stats('unit_stats')
      4     p.strip_dirs()
      5     p.sort_stats('time').print_stats(10)

/usr/local/Cellar/python/2.7.8_1/Frameworks/Python.framework/Versions/2.7/lib/python2.7/cProfile.pyc in run(statement, filename, sort)
     27     try:
     28         try:
---> 29             prof = prof.run(statement)
     30         except SystemExit:
     31             pass

/usr/local/Cellar/python/2.7.8_1/Frameworks/Python.framework/Versions/2.7/lib/python2.7/cProfile.pyc in run(self, cmd)
    133         import __main__
    134         dict = __main__.__dict__
--> 135         return self.runctx(cmd, dict, dict)
    136 
    137     def runctx(self, cmd, globals, locals):

/usr/local/Cellar/python/2.7.8_1/Frameworks/Python.framework/Versions/2.7/lib/python2.7/cProfile.pyc in runctx(self, cmd, globals, locals)
    138         self.enable()
    139         try:
--> 140             exec cmd in globals, locals
    141         finally:
    142             self.disable()

<string> in <module>()

<ipython-input-1-113fd7961f17> in make_plot()
     14     V_g_vals = (1*u.V, 20*u.V, 40*u.V)
     15     gpd.calc_plot_data('jitter', 'd', 'V_g', V_g_vals,
---> 16                        x_scale='linear', n_pts=5)

/Users/ryandwyer/Documents/Programming/mypython/jittermodel/jittermodel/plot.pyc in calc_plot_data(self, y_var, x_var, multi_plot_var, multi_plot_values, x_scale, n_pts)
    155         self.n_pts = n_pts
    156 
--> 157         self._make_sim_array()
    158         self._calculate_plot_points()
    159 

/Users/ryandwyer/Documents/Programming/mypython/jittermodel/jittermodel/plot.pyc in _make_sim_array(self)
    114         """Makes an array of simulations over the variable varied
    115         in the experiment, and also for multi_plot_var."""
--> 116         variable_values = self._calc_variable_array(self.x_scale)
    117         self.all_sims = []
    118         for multi_plot_val in self.multi_plot_values:

/Users/ryandwyer/Documents/Programming/mypython/jittermodel/jittermodel/plot.pyc in _calc_variable_array(self, x_scale, n_pts)
     97         elif x_scale == 'linear':
     98             _r = np.linspace(self.variable_range[0],
---> 99                              self.variable_range[1], n_pts)
    100         else:
    101             raise ValueError("x_scale must be either 'log' or 'linear'")

/usr/local/lib/python2.7/site-packages/numpy/core/function_base.pyc in linspace(start, stop, num, endpoint, retstep, dtype)
     87 
     88     if dtype is None:
---> 89         dtype = result_type(start, stop, float(num))
     90 
     91     if num <= 0:

TypeError: data type not understood

We see that _corr_integrand (3.6), coth (2.845), and _im_dielectric (2.155) take the vast majority of the time . We can try changing this by optimizing these functions. First, let's try removing the coth function call, instead calling 1\tanh.


In [22]:
my_profile()


Thu Feb 27 10:18:35 2014    unit_stats

         5059933 function calls (5031927 primitive calls) in 10.245 seconds

   Ordered by: internal time
   List reduced from 246 to 10 due to restriction <10>

   ncalls  tottime  percall  cumtime  percall filename:lineno(function)
   297900    3.509    0.000    8.891    0.000 usimulation.py:216(_corr_integrand)
   297900    3.461    0.000    5.342    0.000 usimulation.py:181(_im_dielectric)
   893850    0.578    0.000    0.578    0.000 ubase.py:219(E_s)
   298050    0.456    0.000    0.560    0.000 ubase.py:266(kappa)
   297900    0.336    0.000    0.719    0.000 ubase.py:274(E_eff)
     1890    0.250    0.000    9.370    0.005 {scipy.integrate._quadpack._qagie}
   297900    0.229    0.000    9.120    0.000 usimulation.py:230(<lambda>)
   298050    0.170    0.000    0.228    0.000 ubase.py:261(sigma)
   298050    0.147    0.000    0.163    0.000 ubase.py:203(diff)
   596297    0.112    0.000    0.112    0.000 ubase.py:246(rho)


Wow! We see that eliminating this one function call reduced the execution time by 1.6 seconds! Let's try removing some boilerplate type checking in these critical functions.


In [3]:
my_profile()


Thu Feb 27 10:23:39 2014    unit_stats

         5062703 function calls (5034666 primitive calls) in 9.927 seconds

   Ordered by: internal time
   List reduced from 248 to 10 due to restriction <10>

   ncalls  tottime  percall  cumtime  percall filename:lineno(function)
   297900    3.397    0.000    8.560    0.000 usimulation.py:216(_corr_integrand)
   297900    3.323    0.000    5.119    0.000 usimulation.py:181(_im_dielectric)
   893850    0.531    0.000    0.531    0.000 ubase.py:219(E_s)
   298050    0.442    0.000    0.547    0.000 ubase.py:266(kappa)
   297900    0.326    0.000    0.692    0.000 ubase.py:274(E_eff)
     1890    0.240    0.000    9.024    0.005 {scipy.integrate._quadpack._qagie}
   297900    0.224    0.000    8.784    0.000 usimulation.py:230(<lambda>)
   298050    0.164    0.000    0.222    0.000 ubase.py:261(sigma)
   298050    0.136    0.000    0.153    0.000 ubase.py:203(diff)
   596297    0.113    0.000    0.113    0.000 ubase.py:246(rho)



In [9]:
my_profile()


Thu Feb 27 10:09:15 2014    unit_stats

         5655733 function calls (5627727 primitive calls) in 11.800 seconds

   Ordered by: internal time
   List reduced from 247 to 10 due to restriction <10>

   ncalls  tottime  percall  cumtime  percall filename:lineno(function)
   297900    3.574    0.000   10.413    0.000 usimulation.py:216(_corr_integrand)
   595800    2.808    0.000    2.808    0.000 usimulation.py:44(coth)
   297900    2.136    0.000    6.799    0.000 usimulation.py:181(_im_dielectric)
   893850    0.543    0.000    0.543    0.000 ubase.py:219(E_s)
   298050    0.468    0.000    0.575    0.000 ubase.py:266(kappa)
   297900    0.335    0.000    0.712    0.000 ubase.py:274(E_eff)
     1890    0.246    0.000   10.900    0.006 {scipy.integrate._quadpack._qagie}
   297900    0.241    0.000   10.654    0.000 usimulation.py:230(<lambda>)
   298050    0.172    0.000    0.231    0.000 ubase.py:261(sigma)
   298050    0.137    0.000    0.154    0.000 ubase.py:203(diff)



In [ ]: