衡量代码的各个方面
是否有一个工具能够测量项目中函数调用的频率并统计Python代码的其他方面(用于统计目的)?
谢谢
Is 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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
Python 分析器应该为您提供相当多的信息:
您可以使用一个简单的脚本加载结果:
或者,如果您不指定
-o fooprof
,结果将打印到 stdout。请参阅 http://docs.python.org/library/profile.html 中的文档
我不确定您想要计算哪些“其他方面”,但这将决定该函数被调用的次数。如果您对调用的“频率”感兴趣,那么您可以找到作为总执行时间和函数调用次数的函数的平均频率。例如:
假设 foo() 在 10 秒内被调用 100 次。则平均调用频率为 10 次/秒。
The Python profiler should provide you with quite a bit of information:
You load the results using a simple script:
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.我猜你想做静态代码分析。您的代码中有多少个位置
调用一个函数。
这在像 python 这样的动态语言中很难做到,因为有太多
函数可以通过其他方式调用,而不是通过正确的名称,甚至是 python 字节码编译器
并不总是知道某个地方将调用哪个函数,甚至可能会改变
执行期间。还有标准的面向对象多态性。
考虑一下:
任何静态分析工具都无法找出 math.* 中的哪些函数将要执行
被这段代码调用。即使第一个例子也很难推理,
第二个是不可能的。
以下工具对复杂性进行了一些静态分析。
其他分析工具(如 PyLint 和 PyChecker)更侧重于风格和可能
错误。
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:
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.
Other analysis tools like PyLint and PyChecker rather focus on style and probable
errors.
我从未使用过它,但它看起来像 cProfile 可能是一个很好的起点。 (请注意,该页面提到的三个分析器中,一个(hotshot)是实验性的并且没有得到很好的支持,一个(配置文件)“为分析程序增加了显着的开销”,最后一个是 cProfile,所以可能会选择它。)页面顶部提供了
profile.py
和pstats.py
源代码的链接。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
andpstats.py
are provided at the top of the page.