Usually I use shell command time
. My purpose is to test if data is small, medium, large or very large set, how much time and memory usage will be.
Any tools for linux or just python to do this?
Usually I use shell command time
. My purpose is to test if data is small, medium, large or very large set, how much time and memory usage will be.
Any tools for linux or just python to do this?
Have a look at timeit, the python profiler and pycallgraph.
timeit
Essentially, you can pass it python code as a string parameter, and it will run in the specified amount of times and prints the execution time. The important bits from the docs:
... and:
Profiling
Profiling will give you a much more detailed idea about what's going on. Here's the "instant example" from the official docs:
Which will give you:
Both of these modules should give you an idea about where to look for bottlenecks.
Also, to get to grips with the output of
profile
, have a look at this postpycallgraph
This module uses graphviz to create callgraphs like the following:
You can easily see which paths used up the most time by colour. You can either create them using the pycallgraph API, or using a packaged script:
The overhead is quite considerable though. So for already long-running processes, creating the graph can take some time.
The
timeit
module was slow and weird, so I wrote this:Example:
For me, it says:
This is a primitive sort of benchmarking, but it's good enough.
Memory Profiler for all your memory needs.
https://pypi.python.org/pypi/memory_profiler
Run a pip install:
Import the library:
Add a decorator to the item you wish to profile:
Execute the code:
Recieve the output:
Examples are from the docs, linked above.
I usually do a quick
time ./script.py
to see how long it takes. That does not show you the memory though, at least not as a default. You can use/usr/bin/time -v ./script.py
to get a lot of information, including memory usage.I use a simple decorator to time the func
Have a look at nose and at one of its plugins, this one in particular.
Once installed, nose is a script in your path, and that you can call in a directory which contains some python scripts:
This will look in all the python files in the current directory and will execute any function that it recognizes as a test: for example, it recognizes any function with the word test_ in its name as a test.
So you can just create a python script called test_yourfunction.py and write something like this in it:
Then you have to run
and to read the profile file, use this python line: