`
cloudhe
  • 浏览: 107643 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

Amusing Python 2: range/xrange

阅读更多
这两个基本上都是在循环的时候用。
for i in range(0, 100):
    print i
for i in xrange(0, 100):
    print i
  
这两个输出的结果都是一样的,实际上有很多不同,range会直接生成一个list对象:
>>> a = range(0, 10)
>>> type(a)
<type 'list'>
>>> a
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> a[0]
0
 
而xrange则不会直接生成一个list,而是每次调用返回其中的一个值:
>>> a = xrange(0, 10)
>>> type(a)
<type 'xrange'>
>>> a
xrange(10)
>>> a[0]
0
 
所以xrange做循环的性能比range好,尤其是返回很大的时候!

尽量用xrange吧,除非你是要返回一个列表。
 
分享到:
评论
2 楼 hzpfly 2011-07-19  
1 楼 snageyang 2009-03-13  
10000的结果

xrange 0.65599989891  0.546999931335  0.56200003624   0.546999931335

range  0.53200006485  0.56299996376   0.56299996376  0.593999862671

100000的结果

xrange   6.36000013351  5.93799996376

range    6.17199993134  5.5

1000000的结果(没来得及测试)

xrange
range
  

相关推荐

    Geoprocessing-with-Python.pdf

    After amusing myself by automating much of that job, I decided to return to school and study biol- ogy, which is when I took my first GIS course. I was instantly in love, and managed to convince ...

    外研社高中英语必修二单词表

    英文 音标 词性 中文 模块 academic /,ækə'demik/ adj. 学术的 Module 1 province /'prɒvins/ n. 省 enthusiastic /in,θju:zi'æstik/ ...amusing /ə'mju:ziŋ/ adj. 有趣的;可笑的 Module 2

    自己手写的实用python项目-脚本合集-Amusing_python.zip

    2. **数据分析**:Python的`pandas`库是数据处理和分析的强大工具,而`numpy`和`matplotlib`则提供了数值计算和可视化功能。这个合集可能包含了一些数据分析的示例,比如数据清洗、统计分析、图表绘制等。 3. **...

    amusing-x:有趣的-x

    1,先做个基本的功能,展示出几个人们行业有趣的人2,搜索有趣的人 服务划分理念 采用为服务架构,将整个服务架构设定为宇宙模型,每个服务为一个星体,服务启动需要的配置是星体需要的物质组成服务的启动则为星体的...

    Managebaker-crx插件

    https://managebaker.com/discuss/d/2-we-released-version-4-0-0 =============================================== 分析成绩数据,让你更加了解自己,学业畅通无阻。 Analyze your grades to help you understand ...

    Pi: A Source Book, 3rd edition

    Additionally, there is a selection of pieces that are anecdotal, fanciful, or simply amusing. For this new edition, the authors have updated the original material while adding new material of ...

    amusing.py

    amusing.py

    新概念shewasnotamusedPPT课件.pptx

    2. **句子结构与情景**: - 课件中提到的三个时间点(before, after, now)可能与一个环境变化的故事有关,比如火灾前后乡村景象的对比。 - **villagers were very glad**:村民们非常高兴,可能是看到灾后重建或...

    雅思口语必备单词.docx

    2. **听觉词汇**:与声音和听力相关的词汇。 - **birds chirping in the trees**:描述鸟儿在树间歌唱的场景。 - **disturbing noise**:表示恼人的噪音,如交通噪音。 - **pleasing to the ear**:形容悦耳的...

    Pi: A Source Book, 2rd edition

    Pi: A Source Book, 2rd edition 内容简介 · · · · · · This book documents the history of pi from the dawn of mathematical time to the present. One of the beauties of the literature on pi is that ...

    新概念第二册第14课课文讲解.doc

    2. experience(n.):经历、体验 示例:He has a lot of experience.(他有很多经历。) 说明:experience 可以用作不可数名词,表示总的经历或体验,也可以用作可数名词,表示具体的经历或体验。 3. wave(vt....

    b1m2m3m4词汇补充,原因句式.ppt

    2. **原因句式** - `The reason why…is that…` 这个句型用来表达原因。例如: - "我们必须保护环境是因为它对我们非常重要" 可以翻译为 "The reason why we must protect the environment is that it is very ...

    现在分词与过去分词的用法.doc

    2. 作表语:现在分词作表语时,常用来描绘主语的特征,如“amusing, discouraging, puzzling”等,表示主语的特性。例如,“The story is moving.”中的“moving”表示故事有感人之处。 3. 作宾语补足语:当现在...

    Pi: A Source Book

    a variety of historical studies on the cultural significance of the number, and an assortment of anecdotal, fanciful, and simply amusing pieces. For this new edition, the authors have updated the ...

    常见词词形变化.doc

    2. **形容词与副词的转换**: - `angry`(形容词)- `angrily`(副词):表示生气的状态或方式 - `alone`(副词)- `lonely`(形容词/副词):表示独自一人的状态或情感上的孤独 3. **形容词与国籍/地域名称的...

    高效率记住单词新思维英语单词记忆法规则读音与构词法记忆PPT课件.pptx

    - **u[ju:]**:在"consumer", "accuse", "abuse", "amusing"中,字母u发[ju:]音,如句子"Consumers accuse them of abusing power, which is not amusing to them."。 - **u[u:]**:在"conclusion", "crucial", ...

    Pi: A Source Book第三版

    Additionally, there is a selection of pieces that are anecdotal, fanciful, or simply amusing. For this new edition, the authors have updated the original material while adding new material of ...

    小学英语三分钟演讲PPT教案.pptx

    * 介绍了英语句子的使用,例如:"Well, apart from talking, he can do some amusing actions, like dancing." 知识点四:英语演讲技巧 * 讲解了英语演讲技巧,例如:如何使用故事来讲解英语单词和句子结构 * 介绍...

    有趣的狗&小狗 - 有趣的宠物壁纸「Funny Dogs & Puppies - Amusing Pet Wallpapers」-crx插件

    这些酷和有趣的小狗和狗的图像会让你笑!每个新标签都可以欣赏到幽默的高清宠物图像。 狗和幼犬是如此有趣和搞笑,它们从未使我们的生活变得更加愉快。 每次您打开新标签页时,都可以将自己与有趣的狗或小狗壁纸逗乐...

    colinkahn.flux.dispatcher

    colinkahn.flux.... [ " was-funny " " was-mildly-amusing " ] ( do ( wait-for :comedy ) { :audience :laughs })) ( dispatch { :type " was-funny " :who " Jerry " }) @state ; =&gt; {:comedy {:funny tru

Global site tag (gtag.js) - Google Analytics