unix - Is there any simple way to benchmark python script? -
Usually I use shell command time
. My purpose is that if the data is a small, medium, large or very large set, then how much time and memory will be used.
Any device for Linux or just python to do this? Take a look at
, and.
timeit
def test (): "" "stupid test function" "for category (100): lst.append (i) if __name__ == '__main__': Import time print (Time time ("test ()", setup = "__main__ import test"))
Basically, you can call it a Python code of a string parameter And it will run in the specified time and will execute the execution time. Important bits from docs:
timeit.timeit (stmt = 'pass', Setup = 'Pass', Timer = & gt; Default Timer & gt; ;; Number = 1000000)
Create a
timer
example with the given statement, Setup code And run the timer function and number with fusiontimeit
.
. And:
timer. Time (number = 1000000)
time number the execution key statement once it executes the setup statement, and then takes time to execute the key statement, which Many times it is measured as float. The logic is the number of times through the loop, the default for one million. The main statement, the setup statement and the timer function to be used is passed to the constructor.
Note
By default,
timetable
temporarily stops during the timeGarbage Collection
The advantage of this approach is that it makes independent time more comparable The disadvantage is that measuring GC can be an important component of the performance of the work. If so, GC can be re-enabled as the first statement in the Setup string, for example:
timeit.Timer ('xchange (10) I: 'oct (i)', 'gc.enable ()'). Profiling of
Profiling
Profiling will give you more detailed ideas about what's going on . "Instantly example" from here:
import cProfile import again cProfile.run ('re.compile ("foo | times")')
Which will give you:
1 9 7 function calls in 0.007 seconds (1 9 2 artic call) Order by: Standard Name Encolc Totaim Percall cumtime percall filename: Lenino (function) 1 0.000 0.000 0.001 0.001 & lt; String & gt;: 1 (
In both of these modules, you should know where to look for obstacles.
In addition, there is a look at the
to catch with the output of the code pycallgraph
like the callgraph Grapewizes are used to create:
You can easily see which paths use the most time by color, you can either create them using the pycallgraph API, or use a pack script:
< Code> pycallgraph graphviz - ./m Ypythonscript.pyThe overhead is quite enough, however, because for long-running processes already, graph creation can take some time.
Comments
Post a Comment