Should we always favor xrange() over range()

python2.7下range()和xrange()

>>> print range.__doc__
range(stop) -> list of integers
range(start, stop[, step]) -> list of integers
Return a list containing an arithmetic progression of integers.
range(i, j) returns [i, i+1, i+2, …, j-1]; start (!) defaults to 0.
When step is given, it specifies the increment (or decrement).
For example, range(4) returns [0, 1, 2, 3]. The end point is omitted!
These are exactly the valid indices for a list of 4 elements.
>>> print xrange.__doc__
xrange(stop) -> xrange object
xrange(start, stop[, step]) -> xrange object
Like range(), but instead of returning a list, returns an object that generates the numbers in the range on demand. For looping, this is slightly faster than range() and more memory efficient.

从上文看出range()返回一个list,而xrange()返回list中的一个数。所以,当你要在一个大的区间上迭代的时候,性能上xrange()更好,因为不需要一次性产生所有的数。但是在以下几种情况下,还是推荐range()。

  • 在python3中range()做了xrange做的事情,但是xrange不存在,所以你要写在python2和3中都能运行的程序,就尽量用range()。
  • range()在某些场合下会更快,比如,如果你在同一个序列上迭代很多次。xrange()每次都要重建整数对象,但是range()却不用(当然,这种方式会直接导致内存效率下降)。
  • 在真正需要一个list的时候,比如需要应用list的slice特性的时候。

arange


  • arange是numpy包中的一个函数,它类似range()但是并不返回list而是返回numpy.ndarray对象。而且,它不仅限于整数,但是当不是整数的时候用linspace更好,因为arange产生的结果往往会因为浮点数溢出问题而不一致。

>>>print np.arange.__doc__
arange([start,] stop[, step,], dtype=None)
Return evenly spaced values within a given interval.
Values are generated within the half-open interval [start, stop)(in other words, the interval including start but excluding stop).For integer arguments the function is equivalent to the Python built-in
range <http://docs.python.org/lib/built-in-funcs.html&gt;_ function, but returns an ndarray rather than a list.When using a non-integer step, such as 0.1, the results will often notbe consistent. It is better to use linspace for these cases.
Parameters
start : number, optional
Start of interval. The interval includes this value. The default
start value is 0.
stop : number
End of interval. The interval does not include this value, except in some cases where step is not an integer and floating point round-off affects the length of out.
step : number, optional
Spacing between values. For any output out, this is the distance between two adjacent values, out[i+1] - out[i]. The default
step size is 1. If step is specified, start must also be given.
dtype : dtype
The type of the output array. If dtype is not given, infer the data type from the other input arguments.
Returns
arange : ndarray
Array of evenly spaced values.
For floating point arguments, the length of the result is
ceil((stop - start)/step). Because of floating point overflow,
this rule may result in the last element of out being greater
than stop.
See Also
linspace : Evenly spaced numbers with careful handling of endpoints.
ogrid: Arrays of evenly spaced numbers in N-dimensions.
mgrid: Grid-shaped arrays of evenly spaced numbers in N-dimensions.
Examples
>>> np.arange(3)
array([0, 1, 2])
>>> np.arange(3.0)
array([ 0., 1., 2.])
>>> np.arange(3,7)
array([3, 4, 5, 6])
>>> np.arange(3,7,2)
array([3, 5])

参考文献:
http://blog.csdn.net/duankaifei/article/details/43866849
http://stackoverflow.com/questions/135041/should-you-always-favor-xrange-over-range

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值