0

So far, when I want to inspect what might cause some sort of code to run faster compared to a very similar method, I'm using the dis module. However, the further steps of comparing what the causes is basically adding/removing lines.

Is there a more sophisticated way of actually listing what the high-offenders are?

user1767754
  • 23,311
  • 18
  • 141
  • 164
  • Related: [How does one use `dis.dis` to analyze performance?](https://stackoverflow.com/q/19322705/190597) – unutbu May 15 '18 at 02:18

1 Answers1

1

What kind of code do you want to analyze? If you want to analyze pure python code. You can use profile. For example:

import cProfile

cProfile.run("x=1")

Or you can run a function: cProfile.run("function()")

Then it will show you something like the following:

         4 function calls in 0.013 seconds

   Ordered by: standard name

   ncalls  tottime  percall  cumtime  percall filename:lineno(function)
        1    0.013    0.013    0.013    0.013 <ipython-input-7-8201fb940887>:1(fun)
        1    0.000    0.000    0.013    0.013 <string>:1(<module>)
        1    0.000    0.000    0.013    0.013 {built-in method builtins.exec}
        1    0.000    0.000    0.000    0.000 {method 'disable' of '_lsprof.Profiler' objects}
Sraw
  • 18,892
  • 11
  • 54
  • 87