Windows 中的 Python 分析,如何忽略内置函数
我无法在网上找到这个。我一直在寻找如何使用分析器更好地优化我的代码,并且在按累计使用时间最多的函数进行排序时,str()、print 和其他类似的广泛使用的函数占用了配置文件的大部分内容。分析 python 程序以获取用户定义的函数以查看可以优化代码的哪些区域的最佳方法是什么?
我希望这是有道理的,如果您能就这个主题提供任何线索,我们将不胜感激。
I have not been capable of finding this anywhere online. I was looking to find out using a profiler how to better optimize my code, and when sorting by which functions use up the most time cumulatively, things like str(), print, and other similar widely used functions eat up much of the profile. What is the best way to profile a python program to get the user-defined functions only to see what areas of their code they can optimize?
I hope that makes sense, any light you can shed on this subject would be very appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好吧,我假设您的真正目标是使您的代码尽可能快,对吧?
很自然地假设您通过找出函数花费的时间来做到这一点,但还有另一种方式来看待它。
想象一下,当你的程序运行时,它会追踪出一个调用树,这有点像窗外的一棵真实的树。主干就像
main
函数,任何分支从它分离出来就像调用另一个函数。假设每个“叶子”都需要一定的时间,而您想要做的是修剪树,以便删除尽可能多的叶子。
一种方法是找到有很多叶子的树枝并剪掉叶子。另一种方法是,如果不需要的话,将整个树枝剪掉。问题是找到你不需要的重分支。
一种最简单的方法是随机摘下几片叶子,比如 10 片,然后在每片叶子上,沿着树枝一直画一条线,一直回到树干。任何分支点都会有一些这样的线穿过它,从叶子到树干。通过该分支点的线条越多,该分支上的叶子就越多,通过修剪可以节省的时间就越多。
以下是如何将此应用到您的程序中的方法。 示例一片叶子,你随机暂停程序并查看调用堆栈。那是回到行李箱的线。其上的每个调用站点(不是函数,而是调用站点)都是一个分支点。如果该调用站点位于样本的一小部分(例如 40%)上,那么这就是通过修剪它可以节省的大致数量。
因此,不要将其视为衡量函数需要多长时间的方法。可以将其视为询问哪些调用站点“繁重”。这就是全部内容了。
OK, I assume your real goal is to make your code as fast as reasonably possible, right?
It is natural to assume you do that by finding out how long your functions take, but there is another way to look at it.
Consider as your program runs it traces out a call tree, which is kind of like a real tree outside your window. The trunk is like the
main
function, and where any branch splits off from it is like calling another function.Suppose each "leaf" takes a certain amount of time, and what you want to do is prune the tree so as to remove as many leaves as possible.
One way is to find branches with lots of leaves and cut off the leaves. Another way is to cut off whole branches if you don't need them. The problem is to find heavy branches that you don't need.
One bone-simple way to do this is to pick several leaves at random, like 10, and on each one, trace a line down its branch all the way back to the trunk. Any branch point will have some number of these lines running through it, from leaf to trunk. The more lines run through that branch point, the more leaves are on that branch, and the more you could save by pruning it.
Here's how you can apply this to your program. To sample a leaf, you pause the program at random and look at the call stack. That is the line back to the trunk. Each call site on it (not function, call site) is a branch point. If that call site is on some fraction of samples, like 40%, then that is roughly how much you could save by pruning it.
So, don't think of it as measuring how long functions take. Think of it as asking which call sites are "heavy". That's all there is to it.