Is开发者_StackOverflow there a tool that is able to measure the frequency of function calls in a project and counts other aspects (for statistics purposes) of Python code?
thank you
The Python profiler should provide you with quite a bit of information:
python -m cProfile -o fooprof myscript.py
You load the results using a simple script:
#!/usr/bin/env python
import pstats
p = pstats.Stats('fooprof')
p.strip_dirs().sort_stats(-1).print_stats()
Alternatively, if you do not specify -o fooprof
the results are printed to stdout.
See the documentation at http://docs.python.org/library/profile.html
I'm not sure what "other aspects" you are wanting to count, but this will determine how many times the function is called. If you are interested in the "frequency" of calls, then you can find the average frequency as a function of total execution time and the number of times the function was called. For example:
Suppose foo()
was called 100 times in 10 seconds. The average frequency of calls is then 10/second.
I guess you want to do static code analysis. How many locations in your code call a function.
This is very hard to do in dynamic languages like python, because there are so many ways functions may be called otherwise than by proper name, and even the python bytecode compiler won't know always which function is going to be called in a place, and it may even change during execution. And there's standard OO polymorphism too.
Consider:
def doublefx(f, x):
return f(x)*2
print doublefx(math.sqrt, 9) # 6
f = stdin.readline()
print doublefx(getattr(math, f), 9) # ?
No way any static analyses tool will find out which functions in math.* are going to be called by this code. Even the first example would be very hard to reason about, the second is impossible.
The following tool does some static analysis regarding complexity.
- http://sourceforge.net/projects/pymetrics/
Other analysis tools like PyLint and PyChecker rather focus on style and probable errors.
I've never used it, but it looks like cProfile might be a good place to start. (Note that of the three profilers mentioned on that page, one (hotshot) is experimental and not well-supported, one (profile) "adds significant overhead to profiled programs", and the last is cProfile, so probably go with that.) Links to the source code for profile.py
and pstats.py
are provided at the top of the page.
精彩评论