如何分析我的代码? [英] How to profile my code?

查看:49
本文介绍了如何分析我的代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道如何分析我的代码.

I want to know how to profile my code.

我已经阅读了文档,但由于没有给出示例,我无法从中获得任何信息.

I have gone through the docs, but as there were no examples given I could not get anything from it.

我有一个很大的代码,它花费了很多时间,因此我想分析并提高它的速度.我还没有在方法中编写我的代码,中间很少但不完全.我的代码中没有任何主要内容.我想知道如何使用分析.我正在寻找一些关于如何分析的示例或示例代码.

I have a large code and it is taking so much time, hence I want to profile and increase its speed. I havent written my code in method, there are few in between but not completely. I don't have any main in my code. I want to know how to use profiling. I'm looking for some example or sample code of about how to profile.

我尝试了 psyco,即只是在我的代码顶部添加了两行:

I tried psyco, i.e just addded two lines at the top of my code:

import psyco
psyco.full()

这样对吗?它没有显示出任何改善.任何其他加速方式,请提出建议.

Is this right? It did not show any improvement. Any other way of speeding up, please suggest.

推荐答案

这个问题的标准答案是使用 cProfile.

The standard answer to this question is to use cProfile.

您会发现如果不将您的代码分离成方法,cProfile 不会为您提供特别丰富的信息.

相反,您可能想尝试此处另一张海报所说的蒙特卡罗分析.引用另一个答案:

Instead, you might like to try what another poster here calls Monte Carlo Profiling. To quote from another answer:

如果你赶时间,你可以手动中断你的程序调试器主观上很慢,有一个简单的查找性能问题的方法.

If you're in a hurry and you can manually interrupt your program under the debugger while it's being subjectively slow, there's a simple way to find performance problems.

只需暂停几次,每次有时间看看调用栈.如果有是一些正在浪费一些代码时间的百分比,20% 或 50% 或无论如何,这是概率你会在每个动作中捕捉到它 大概就是这样样本的百分比会看到的.没有受过教育的需要猜测.如果你有一个猜猜是什么问题,这个将证明或反驳它.

Just halt it several times, and each time look at the call stack. If there is some code that is wasting some percentage of the time, 20% or 50% or whatever, that is the probability that you will catch it in the act on each sample. So that is roughly the percentage of samples on which you will see it. There is no educated guesswork required. If you do have a guess as to what the problem is, this will prove or disprove it.

你可能有多种表现不同大小的问题.如果你清除其中任何一个,剩下的将需要更大的百分比,并且更容易发现后续传球.

You may have multiple performance problems of different sizes. If you clean out any one of them, the remaining ones will take a larger percentage, and be easier to spot, on subsequent passes.

警告:程序员往往是对这种技术持怀疑态度,除非他们自己用过.他们会说分析器给你这个信息,但只有当他们对整个调用堆栈进行采样.调用图不会给你同样的结果信息,因为 1) 他们没有在教学层面总结,2)他们给出了令人困惑的总结在存在递归的情况下.他们还会说它只适用于玩具程序,当它实际工作时任何程序,它似乎工作在更大的程序上更好,因为他们往往有更多的问题要解决找到[强调].

Caveat: programmers tend to be skeptical of this technique unless they've used it themselves. They will say that profilers give you this information, but that is only true if they sample the entire call stack. Call graphs don't give you the same information, because 1) they don't summarize at the instruction level, and 2) they give confusing summaries in the presence of recursion. They will also say it only works on toy programs, when actually it works on any program, and it seems to work better on bigger programs, because they tend to have more problems to find [emphasis added].

这不是正统的,但我在一个项目中非常成功地使用了它,使用 cProfile 进行分析没有给我有用的输出.

It's not orthodox, but I've used it very successfully in a project where profiling using cProfile was not giving me useful output.

最好的一点是,这在 Python 中非常容易实现.只需在解释器中运行您的 Python 脚本,按 [Control-C],注意回溯并重复多次.

The best thing about it is that this is dead easy to do in Python. Simply run your Python script in the interpreter, press [Control-C], note the traceback and repeat a number of times.

这篇关于如何分析我的代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆