`
cloudhe
  • 浏览: 107123 次
  • 性别: 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

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

    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

    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 ...

    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 ...

    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 ...

    有趣的狗&小狗 - 有趣的宠物壁纸「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

    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 ...

    Pi: A Source Book, 1rd 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 ...

    Curtain.js.zip

    This plugin allows you to create a web page with multiple fixed panels that unroll with an amusing effect. Exactly like a curtain rises. To navigate, you can use your keyboard instead the scrollbar or...

    Algorithms-and-Data-Structures-training---IEEE-CS-ZSB:IEEE CS任务

    amusing_joke.py 潘格拉姆 pangram.py 登记系统 registration_system.cpp 汤姆·里德尔斯日记 tom_riddles_diary.cpp 小吃塔 小吃塔 叶芳芳地址 defanging_an_ip_address.cpp 霍夫曼解码 huffman_decoding.cpp ...

    PI A Biography of the World's Most Mysterious Number

    n this delightful layperson's introduction to one of math's most interesting phenomena, Drs Posamentier and Lehmann review p's history from pre-biblical times to the 21st century, the many amusing and...

Global site tag (gtag.js) - Google Analytics