如何衡量的蟒蛇算法的运行时间 [英] how to measure running time of algorithms in python

查看:328
本文介绍了如何衡量的蟒蛇算法的运行时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能显示的文件:
  在Python函数
准确计时   <一href="http://stackoverflow.com/questions/1685221/accurately-measure-time-python-function-takes">accurately测量时间蟒蛇功能需要

Possible Duplicates:
Accurate timing of functions in python
accurately measure time python function takes

如何MESURE和比较我的算法用Python编写的运行时间。也指向我一个不错的算法网站/论坛,像计算器,如果你能。

How can i mesure and compare the running times of my algorithms written in python .Also point me to a nice algorithms site/forum like stackoverflow if you can.

推荐答案

我不是100%肯定什么是跑我的算法用Python编写的时代的意思,所以我想我可能会尝试提供了一个更广阔的看一些潜在的答案。

I am not 100% sure what is meant by "running times of my algorithms written in python", so I thought I might try to offer a broader look at some of the potential answers.

  • 算法没有运行时间;实现可定时,而是一种算法是一种抽象的方式来做事。最常见和最优化的程序往往是最有价值的部分是分析算法,通常使用渐进分析和计算< A HREF =htt​​p://en.wikipedia.org/wiki/Big_O_notation>大O 在时间,空间,磁盘使用情况等的复杂性。

  • Algorithms don't have running times; implementations can be timed, but an algorithm is an abstract approach to doing something. The most common and often the most valuable part of optimizing a program is analyzing the algorithm, usually using asymptotic analysis and computing the big O complexity in time, space, disk use and so forth.

一个电脑真的不能为你做这一步。这需要做数学找出一些东西是如何工作的。优化的事情这一面的主要成分是一种具有可扩展的性能。

A computer cannot really do this step for you. This requires doing the math to figure out how something works. Optimizing this side of things is the main component to having scalable performance.

你可以在时间的具体实施。要做到这一点在Python的最好方法是使用的 timeit 。它似乎最想使用的方法是使用一个函数封装你想打电话与蟒蛇-m timeit ...

You can time your specific implementation. The nicest way to do this in Python is to use timeit. The way it seems most to want to be used is to make a module with a function encapsulating what you want to call and call it from the command line with python -m timeit ....

使用timeit比较多个片段做microoptimization的时候,但往往不是你想要正确的工具,比较两种不同的算法。这是常见的,你想要的是渐进的分析,但很有可能你想更复杂的类型的分析。

Using timeit to compare multiple snippets when doing microoptimization, but often isn't the correct tool you want for comparing two different algorithms. It is common that what you want is asymptotic analysis, but it's possible you want more complicated types of analysis.

你必须知道什么时间最片段是不值得改进。的你需要做出改变,他们其实算,尤其的,当你在做微的优化,而不是提高你的算法的渐进复杂。

You have to know what to time. Most snippets aren't worth improving. You need to make changes where they actually count, especially when you're doing micro-optimisation and not improving the asymptotic complexity of your algorithm.

如果您翻两番一个功能,其中的code花费的时间1%的速度,这不是一个真正的加速。如果您在其中的程序花费时间的50%的功能的20%的速度递增,你有真正的收获。

If you quadruple the speed of a function in which your code spends 1% of the time, that's not a real speedup. If you make a 20% speed increase on a function in which your program spends 50% of the time, you have a real gain.

要确定一个真正的Python程序所花费的时间,使用 STDLIB分析公用事业。这将告诉你在哪里在一个示例程序的code花费的时间。

To determine the time spent by a real Python program, use the stdlib profiling utilities. This will tell you where in an example program your code is spending its time.

这篇关于如何衡量的蟒蛇算法的运行时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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