zoukankan      html  css  js  c++  java
  • 实验比较python中的range和xrange

    1 结论: 全用xrange,除非你需要使用返回的列表

    2 实验一:性能对比

    实验环境:win7 ,64位系统 python2.7 

    import time
    StartTime=time.time()
    count =0
    for i in range (100000000): #  这里的8个零  内存变动峰值2555,000kb ,耗时 129s
        count=count+1
    EndTime=time.time()
    print "cost time is:",(EndTime-StartTime)
    import time
    StartTime=time.time()
    count =0
    for i in xrange (100000000): #  这里的8个零  内存3832kb ,耗时 8s
        count=count+1
    EndTime=time.time()
    print "cost time is:",(EndTime-StartTime)

    3 实验二 原理探究

    a = range(5)
    print type(a)          # 输出 <type 'list'>
    print a                #输出 [0 1 2 3 4 ]
    print a[0], a[1]       #输出 0 1
    a = xrange(5)
    print type(a)       # 输出 <type 'xrange'>
    print a                # 输出  xrange(5)
    print a[0], a[1]    #输出 0 1 

    解释:range 会生成一个list ,而 xrange 每次调用返回一个 “xrange object” 。

            实验1 的数据是三次实验,取平均值的结果且数据间的差异不大。

  • 相关阅读:
    循序渐进做项目系列(3):迷你QQ篇(1)——实现客户端互相聊天
    清明时节欲断魂——未知死焉知生?——向死而生!
    curl基本使用
    some tools
    redis源码学习
    设计模式
    object-c基础
    python库
    awk命令
    gcc编译
  • 原文地址:https://www.cnblogs.com/hans-201506/p/4937234.html
Copyright © 2011-2022 走看看