line_profiler

profileを取りたい関数, メソッドに @profile デコレータをつける. (import はいらない)

kernprof -l main.py
python -m line_profiler main.py.lprof
Total time: 0.113609 s
File: src/mysys.py
Function: greens_function at line 254

Line #      Hits         Time  Per Hit   % Time  Line Contents
==============================================================
   254                                               @profile
   255                                               def greens_function(self, sys, energy=0, args=(),
   256                                                                   out_leads=None, in_leads=None, check_hermiticity=True,
   257                                                                   params=None):
   258
   259         1          3.0      3.0      0.0          syst = sys  # ensure consistent naming across function bodies
   260
   261         1          3.0      3.0      0.0          n = len(syst.lead_interfaces)
   262         1          1.0      1.0      0.0          if in_leads is None:
   263         1          4.0      4.0      0.0              in_leads = list(range(n))
   264                                                   else:
   265                                                       in_leads = list(in_leads)
   266         1          1.0      1.0      0.0          if out_leads is None:
   267         1          2.0      2.0      0.0              out_leads = list(range(n))
   268                                                   else:
   269                                                       out_leads = list(out_leads)
   270         1       3362.0   3362.0      3.0          if (np.any(np.diff(in_leads) <= 0) or np.any(np.diff(out_leads) <= 0)):
   271                                                       raise ValueError("Lead lists must be sorted and "
   272                                                                        "with unique entries.")
   273         1          2.0      2.0      0.0          if len(in_leads) == 0 or len(out_leads) == 0:
   274                                                       raise ValueError("No output is requested.")
   275
   276         2      97375.0  48687.5     85.7          linsys, _ = self._make_linear_sys(syst, in_leads, energy, args,
   277         1          1.0      1.0      0.0                                            check_hermiticity, True,
   278         1          1.0      1.0      0.0                                            params=params)
   279
   280         2         12.0      6.0      0.0          kept_vars = np.concatenate([coords for i, coords in   281         1          2.0      2.0      0.0                                      enumerate(linsys.indices) if i in
   282                                                                               out_leads])
   283
   284         1         97.0     97.0      0.1          print(linsys.rhs, len(linsys.rhs))
   285         1        103.0    103.0      0.1          print(linsys.rhs[0].shape, linsys.rhs[1].shape)
   286
   287                                                   # See comment about zero-shaped sparse matrices at the top of common.py.
   288         2        367.0    183.5      0.3          rhs = sp.bmat([[i for i in linsys.rhs if i.shape[1]]],
   289         1          1.0      1.0      0.0                        format=self.rhsformat)
   290         1       2723.0   2723.0      2.4          flhs = self._factorized(linsys.lhs)
   291         1       9548.0   9548.0      8.4          data = self._solve_linear_sys(flhs, rhs, kept_vars)