Python 中什么更快,“while”或“for xrange”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1377429/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 22:05:22  来源:igfitidea点击:

What is faster in Python, "while" or "for xrange"

pythonmicro-optimization

提问by bocco

We can do numeric iteration like:

我们可以进行数字迭代,例如:

for i in xrange(10):
    print i,

and in C-style:

和 C 风格:

i = 0
while i < 10:
    print i,
    i = i + 1

Yes, I know, the first one is less error-prone, more pythonic but is it fast enough as C-style version?

是的,我知道,第一个更不容易出错,更像 Python,但它是否像 C 风格版本一样快?

PS. I'm from C++ planet and pretty new on Python one.

附注。我来自 C++ 星球,对 Python 一窍不通。

回答by u0b34a0f6ae

I am sure the whileversion is slower. Python will have to lookup the add operation for the integer object on each turn of the loop etc, it is not pure C just because it looks like it!

我确定while版本较慢。Python 将不得不在循环的每一轮等处查找整数对象的加法操作,它不是纯 C 只是因为它看起来像!

And if you want a pythonic version of exactly the above, use:

如果你想要一个完全相同的 pythonic 版本,请使用:

print " ".join(str(i) for i in xrange(10))


Edit: My timings look like this. This is just a silly running loop without printing, just to show you what writing out "i += 1" etc costs in Python.

编辑:我的时间看起来像这样。这只是一个没有打印的愚蠢的运行循环,只是为了向您展示在 Python 中写出“i += 1”等的成本。

$ python -mtimeit "i=0" "while i < 1000: i+=1"
1000 loops, best of 3: 303 usec per loop
$ python -mtimeit "for i in xrange(1000): pass"
10000 loops, best of 3: 120 usec per loop

回答by J S

Who cares? Seriously. If you want to know, use timeit package (you can invoke it from command line with -m).

谁在乎?严重地。如果您想知道,请使用 timeit 包(您可以使用 -m 从命令行调用它)。

But it doesn't matter at all, because the difference is negligible. And in general, Python is not a language that you choose if you want speed.

但这根本无关紧要,因为差异可以忽略不计。一般来说,如果你想要速度,Python 不是你可以选择的语言。

回答by Lakshman Prasad

The first one.

第一个。

You mean, faster to develop, right?

你的意思是,开发速度更快,对吧?

PS: It doesn't matter, machines these days are so fast that it is meaningless to ponder on micro optimizations, prior to identifying the bottlenecks using a thorough profiler.

PS:没关系,现在的机器太快了,在使用彻底的分析器确定瓶颈之前,考虑微优化是没有意义的。

回答by rob

They are bothto avoid :-)

他们应该避免:-)

Generally speaking, each time I see an iteration over numbers, I see some non-pythonic code, that could be expressed in a better way using iterations over lists or generators.
Actually, I've said "pythonic", but it is all about readability. Using idiomatic code will increase readability, and ultimately also performance, because the compiler will better know how to optimize it.

一般来说,每次我看到对数字的迭代时,我都会看到一些非 Python 代码,可以使用对列表或生成器的迭代以更好的方式表达。
实际上,我已经说过“pythonic”,但这完全是关于可读性的。使用惯用代码将提高可读性,最终也会提高性能,因为编译器会更好地知道如何优化它。

回答by user9876

If your program is too slow, try using psyco.

如果您的程序太慢,请尝试使用psyco

Don't worry about the kind of micro-optimisation in your question. Write your program to be maintainable (which includes following standard Python style so other programmers can read it easier).

不要担心您的问题中的那种微观优化。编写可维护的程序(包括遵循标准 Python 风格,以便其他程序员可以更轻松地阅读)。

回答by ooboo

In Python, the shorter and clearer version is always better. If I am not mistaken the range and xrange functions are not native, if you try xrange(sys.maxint+1) you will get an overflow error.

在 Python 中,更短、更清晰的版本总是更好。如果我没有弄错 range 和 xrange 函数不是本机的,如果你尝试 xrange(sys.maxint+1) 你会得到一个溢出错误。

Besides, what the hell could this be useful for? If you are just printing 10 numbers, then surely readability counts a thousand times more - and I don't think you're going to print over a million numbers...

此外,这他妈的有什么用?如果您只是打印 10 个数字,那么可读性肯定会增加一千倍 - 我认为您不会打印超过一百万个数字......

回答by Jose

Well, if you are after efficiency in numerical code, you ought to use numpyand scipy. Your integration can be quickly written as numpy.sum( numpy.arange( 10 ) )

好吧,如果你追求数字代码的效率,你应该使用numpyscipy。您的集成可以快速写为numpy.sum( numpy.arange( 10 ) )