一般数字Python问题 [英] General Numerical Python question
问题描述
一般来说,如果有一个列表(来自常规Python)和一个
数组(来自Numerical Python),它包含相同数量的
元素,那么循环或For循环处理它们的速度是否相同?b $ b?或者,阵列处理速度会更快吗?
我是Python的新手,所以我的问题可能会暴露我的无知。我赞赏任何人帮助我理解的努力。
谢谢。非常感谢。
Matt
2mc写道:
一般来说,如果有一个列表(来自常规Python)和一个
数组(来自Numerical Python)包含相同数量的
元素,那么While循环或For循环过程他们以同样的速度?或者,数组处理速度会更快吗?
我是Python的新手,所以我的问题可能会暴露我的无知。我很欣赏任何人帮助我理解的努力。
我不知道,我从未测量过。让我们一起来看看。
回答这些性能问题的最好方法,根据您的平台和确切版本,这可能很容易因为b $ b而变化很小
涉及,是_measure_。 Python 2.3的标准库附带了
timeit.py,这是一个专为此而制作的小脚本。我把它复制到了我的
~ / bin /目录并完成了一个chmod + x(从一个shebang行开始
这样就足够了),或者在Windows中,您可以设置.bat或.cmd
文件来调用Python。无论如何,它很容易使用:你指定零
或更多-s''blahblah''参数设置,然后是你想要的特定
语句时间。观看......:
[alex @ lancelot pop]
timeit.py -s''import数字''-s''x = Numeric.arange(555)''
''for x in x:id(i)''
1000循环,最佳3:296 usec每循环
[alex @ lancelot pop]
timeit.py -s''import数字''-s''x =范围(555)''''我在
x:id(i)''
1000循环,最佳3:212每循环usec
[alex @ lancelot pop]
Generally speaking, if one had a list (from regular Python) and an
array (from Numerical Python) that contained the same number of
elements, would a While loop or a For loop process them at the same
speed? Or, would the array process faster?
I''m new to Python, so my question may expose my ignorance. I
appreciate anyone''s effort to help me understand.
Thanks. It is much appreciated.
Matt
2mc wrote:
Generally speaking, if one had a list (from regular Python) and an
array (from Numerical Python) that contained the same number of
elements, would a While loop or a For loop process them at the same
speed? Or, would the array process faster?
I''m new to Python, so my question may expose my ignorance. I
appreciate anyone''s effort to help me understand.
I don''t know, I''ve never measured. Let''s find out together.
The best way to answer these performance questions, which may
easily vary a little depending on your platform and exact versions
involved, is to _measure_. Python 2.3''s standard library comes with
timeit.py, a little script that''s made just for that. I''ve copied it to my
~/bin/ directory and done a chmod +x (it starts with a shebang line
so that''s sufficient), or in Windows you might set up a .bat or .cmd
file to call Python on it. Anyway, it''s easy to use: you specify zero
or more -s ''blahblah'' arguments to set things up, then the specific
statement you want to time. Watch...:
[alex@lancelot pop]
timeit.py -s''import Numeric'' -s''x=Numeric.arange(555)''
''for i in x: id(i)''
1000 loops, best of 3: 296 usec per loop
[alex@lancelot pop]
timeit.py -s''import Numeric'' -s''x=range(555)'' ''for i in
x: id(i)''
1000 loops, best of 3: 212 usec per loop
[alex@lancelot pop]
这篇关于一般数字Python问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!