Difference between Range and Xrange in Python

原创 2015年11月18日 15:13:18

概述

之前在stackoverflow上看见一个答案描述Python中range与xrange的区别:
range的返回值是一个list,包含所有范围内满足条件的元素;
而Xrange的返回值是一个xrange的Object,通过协程,在每次调用时运行一次生成下一个元素的对象。
(在文档中可以找到类似的表述)

也就是说range是一次生成所有元素并返回list,而xrange是在每次调用时生成一个;换言之,xrange对内存会更加友好。


举一个栗子

在了解这个答案后我的印象中一直认为xrange貌似要比range好一点,不过一直没有一个直观的印象。
直到最近尝试做ProjectEuler的一个题:Highly divisible triangular number

无脑地直接写了一个暴力的解法:

triangular = 0
triangular_step = 0


def triangular_num():
    global triangular_step, triangular
    # make step add 1, and figure out triangular num
    triangular_step += 1
    triangular += triangular_step


def divisor_num(digit):
    num = 0
    for tmp in range(1, digit + 1):
        if digit % tmp == 0:
            num += 1
    return num


if __name__ == "__main__":
    while True:
        triangular_num()
        dn = divisor_num(triangular)
        with open('./out.txt', 'a') as f:
            f.write(str(triangular) + ' : ' + str(dn) + '\n')
        if dn > 500:
            break

运行时发现内存的使用量高达几百,并且很稳定地向上增长:
python2.7-range

之后我将range改为xrange:
python2.7-xrange
内存由此稳定在3.5M左右。


注与吐槽

说明一下,笔者使用的Python是2.7的版本,在Python3中range的功能已由xrange替换,所以你可以放心大胆地用range而不用担心内存的问题。

吐槽:
笔者也想用Python3,python3中很多特性都非常好,不过无奈更新太频繁,很多组件都不支持。
学习中的一点小体会,写出来与大家分享一下。

版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

python中下划线,双下划线,首尾双下划线的区别(Difference between _, __ and __xx__ in Python)

Difference between _, __ and __xx__ in Python When learning Python many people don't really under...
  • Sinchb
  • Sinchb
  • 2013年03月13日 13:24
  • 2830

Difference between using bean id and name in Spring configuration file

Question: Is there any difference between using an id attribute and name attribute on a  elemen...
  • aqzwss
  • aqzwss
  • 2016年08月23日 11:34
  • 205

What is the difference between getWidth/heigth() and getMeasuredWidth/Heigth() in Android SDK?

From: http://stackoverflow.com/questions/8657540/what-is-the-difference-between-getwidth-heigth-and-...
  • fyfcauc
  • fyfcauc
  • 2014年10月11日 16:54
  • 389

Condition variables in Golang difference between Signal and Broadcast

Golang 标准库中提供了sync.Mutex 用于多线程之间的同步。 同时也提供了条件变量配合Mutex结合使用。 条件变量主要的使用场景是: 当线程要执行A操作时,条件B未满足,而无法执...

Difference between prototype and this in JavaScript

When I’m studying JavaScript, I got a little bit confused while I first time look at function.protot...

What is the difference between BIT and TINYINT in MySQL

What is the difference between BIT and TINYINT in MySQL? A TINYINT is an 8-bit integer value, a...

Difference between release and dealloc in objective-c

转载自:http://stackoverflow.com/questions/559295/difference-between-release-and-dealloc-in-objective-c ...

What is difference between HashMap and Hashtable in Java?

HashMap vs Hashtable in Java Though both Hashtable and HashMap are data-structure based upon hashin...
  • e891377
  • e891377
  • 2017年05月23日 23:14
  • 168

Difference between ArrayList and Vector In java

Difference between ArrayList and Vector In java ArrayList and Vector both use Array as a data struc...

What's the difference between “local” and “my” in Perl

There are two kinds of variable scopes in Perl: Global variables: They reside in the current pa...
  • ThinkHY
  • ThinkHY
  • 2012年08月30日 12:55
  • 1404
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:Difference between Range and Xrange in Python
举报原因:
原因补充:

(最多只允许输入30个字)