使用xrange

 

当我们获取某个数量的循环时,我们惯用的手法是for循环和range函数,例如:

for i in range(10):
print i

这里range(10)生成了一个长度为10的列表,内容为从0到9,所以这里的for循环实际上是在遍历其中的元素。

如果循环次数过大的时候,range要生成一个巨大的列表,这将导致程序的性能降低。

解决方案是采用xrange,用法基本与range相同:

for i in xrange(10):
print i

但是二者的性能差距到底有多大?

 

性能测评

 

我们使用下面的程序做一个测试:

from time import time
from time import sleep
import sys def count_time():
def tmp(func):
def wrapped(*args, **kargs):
begin_time = time()
result = func(*args, **kargs)
end_time = time()
cost_time = end_time - begin_time
print '%s called cost time : %s ms' %(func.__name__, float(cost_time)*1000)
return result
return wrapped
return tmp @count_time()
def test1(length):
for i in range(length):
pass @count_time()
def test2(length):
for i in xrange(length):
pass if __name__ == '__main__':
length = int(sys.argv[1])
test1(length)
test2(length)

上面的代码中,count_time是一个装饰器,用于统计程序运行的时间。

我们下面开始正式的测试:

wing@ubuntu:~/Documents/py|⇒  python 10.py 100000
test1 called cost time : 13.8590335846 ms
test2 called cost time : 3.76796722412 ms
wing@ubuntu:~/Documents/py|⇒ python 10.py 100000
test1 called cost time : 16.725063324 ms
test2 called cost time : 3.08418273926 ms
wing@ubuntu:~/Documents/py|⇒ python 10.py 200000
test1 called cost time : 34.875869751 ms
test2 called cost time : 7.85899162292 ms
wing@ubuntu:~/Documents/py|⇒ python 10.py 500000
test1 called cost time : 41.6638851166 ms
test2 called cost time : 17.1940326691 ms
wing@ubuntu:~/Documents/py|⇒ python 10.py 500000
test1 called cost time : 59.8731040955 ms
test2 called cost time : 14.0538215637 ms
wing@ubuntu:~/Documents/py|⇒ python 10.py 500000
test1 called cost time : 94.1109657288 ms
test2 called cost time : 8.5780620575 ms
wing@ubuntu:~/Documents/py|⇒ python 10.py 500000
test1 called cost time : 61.615228653 ms
test2 called cost time : 7.21502304077 ms

结果令我们大吃一惊,二者的差距非常明显,最高的时候差距了十几倍。

我们再选取几个较小的数据:

wing@ubuntu:~/Documents/py|⇒  python 10.py 10
test1 called cost time : 0.00596046447754 ms
test2 called cost time : 0.0109672546387 ms
wing@ubuntu:~/Documents/py|⇒ python 10.py 20
test1 called cost time : 0.00619888305664 ms
test2 called cost time : 0.159025192261 ms
wing@ubuntu:~/Documents/py|⇒ python 10.py 50
test1 called cost time : 0.00786781311035 ms
test2 called cost time : 0.00405311584473 ms
wing@ubuntu:~/Documents/py|⇒ python 10.py 100
test1 called cost time : 0.00786781311035 ms
test2 called cost time : 0.00309944152832 ms

这次range的性能并不差,甚至开始还略显高。

我们可以得出结论,当n较小时,我们使用range,但当i超过一定范围时,我们就必须考虑使用xrange了

但是,二者性能差距的原因在哪里?

我们下文分析。

从range和xrange的性能对比到yield关键字(上)的更多相关文章

  1. 从range和xrange的性能对比到yield关键字(中)

    上节提出了range和xrange的效率问题,这节我们来探究其中的原因   yield的使用   我们看下面的程序: #coding: utf-8 def test(): print 4 print ...

  2. [Python]range与xrange用法对比

    [整理内容]具体如下: 先来看如下示例:>>>x=xrange(0,8)>>> print xxrange(8)>>>print x[0]0> ...

  3. 实验比较python中的range和xrange

    1 结论: 全用xrange,除非你需要使用返回的列表 2 实验一:性能对比 实验环境:win7 ,64位系统 python2.7 import time StartTime=time.time() ...

  4. Python从题目中学习:range()和xrange()

    近期给公司培训Python,好好啃了啃书本,查了查资料,总结一些知识点. --------------------------------------------------------------- ...

  5. python 中range与xrange的区别

    先来看看range与xrange的用法介绍 help(range)Help on built-in function range in module __builtin__: range(...) r ...

  6. range和xrange的区别详解

    两种用法介绍如下:1.range([start], stop[, step])返回等差数列.构建等差数列,起点是start,终点是stop,但不包含stop,公差是step.start和step是可选 ...

  7. Suspend to RAM和Suspend to Idle分析,以及在HiKey上性能对比【转】

    转自:https://www.cnblogs.com/arnoldlu/p/6253665.html 测试环境:AOSP 7.1.1+Kernel 4.4.17 HW:HiKey Ubuntu 14. ...

  8. range与xrange的区别

    一.Python中range()与xrange()有什么区别 range([start,] stop[, step]),根据start与stop指定的范围以及step设定的步长,生成一个序列 rang ...

  9. python中range、xrange和randrange的区别

    range 函数说明:range([start,] stop[, step]),根据start与stop指定的范围以及step设定的步长,生成一个列表. xrange 函数说明:和range 的用法完 ...

随机推荐

  1. 【译】RabbitMQ:"Hello World"

    简介 RabbitMQ是一个消息代理.从本质上讲,它从消息生产者处接收消息,然后传递给消息的消费者.它在消息的生产者和消费者之间根据你指定的规则对消息进行路由.缓存和持久化. RabbitMQ通常使用 ...

  2. MongoDB文档、集合、数据库简介

    文档 概述 文档是MongoDB的核心概念,是数据的基本单元,非常类似于关系数据库中的行.在MongoDB中,文档表示为键值对的一个有序集.MongoDB使用Javascript shell,文档的表 ...

  3. Java核心知识点学习----线程同步工具类,CyclicBarrier学习

    线程同步工具类,CyclicBarrier日常开发较少涉及,这里只举一个例子,以做备注.N个人一块出去玩,相约去两个地方,CyclicBarrier的主要作用是等待所有人都汇合了,才往下一站出发. 1 ...

  4. sql 返回xml类型的数据

    1, 这中方式可以在Item节点上加一个Items节点作为所有item节点的父节点 SELECT  Orders.OrderNumber ,        ( SELECT    ProductID ...

  5. php 递归函数的三种实现方式

    递归函数是我们常用到的一类函数,最基本的特点是函数自身调用自身,但必须在调用自身前有条件判断,否则无限无限调用下去.实现递归函数可以采取什么方式呢?本文列出了三种基本方式.理解其原来需要一定的基础知识 ...

  6. Linux云服务器安装tomcat

    安装tomcat需要安装JDK 1.上传 把下载好的tomcat压缩包(apache-tomcat-7.0.tar.gz)和jdk(jdk-7u76-linux-x64.tar.gz)压缩包上传到/u ...

  7. PHP-redis中文文档-命令

    关于redis of php的安装,详见自己的为知笔记,其中包含: 1.php版本的redis的安装 2.redis的使用方法 3.redis带conf文件的启动 这里介绍redis的命令,php版本 ...

  8. 【Java学习笔记】Map集合的keySet,entrySet,values的用法例子

    import java.util.Collection; import java.util.HashMap; import java.util.Iterator; import java.util.M ...

  9. [2011山东ACM省赛] Sequence (动态规划)

    Sequence Time Limit: 1000ms   Memory limit: 65536K  有疑问?点这里^_^ 题目描述 Given an integer number sequence ...

  10. linux httpd 服务的安装

    1.查看是否安装了httpd rpm -qa|grep httpd 2.安装httpd 使用yum 安装 yum -y install httpd 3.关闭防火墙和selinxu 4.使用fz软件或者 ...