1 、背景:

想知道5种遍历方法,并且知道从性能角度考虑,使用哪种。

2、结论:

使用这种方式:

    for key,val in AutoDict.iteritems():
temp = "%s:%s" % (key,val)

  不用这种方式:

    for (key,val) in AutoDict.items():
temp = "%s:%s" % (key,val)

实验:

import time
def testDict():
count = 1000000
AutoDict = {}
for i in xrange(count):
AutoDict[i] = i
startTime = time.time()
for d in AutoDict:
temp = "%s:%s" % (d,AutoDict[d])
#method 1
print 'span time 1 :%s' % str(time.time()-startTime)
startTime = time.time()
for (key,val) in AutoDict.items():
temp = "%s:%s" % (key,val)
#method 2
print 'span time 2 :%s' % str(time.time()-startTime)
startTime = time.time()
for key,val in AutoDict.iteritems():
temp = "%s:%s" % (key,val)
#method 3
print 'span time 3 :%s' %str(time.time()-startTime)
startTime = time.time()
for key in AutoDict.iterkeys():
temp = "%s:%s" %(key,AutoDict[key])
#method 4
print 'span time 4 :%s' %str(time.time()-startTime)
startTime = time.time()
for key,val in zip(AutoDict.iterkeys(),AutoDict.itervalues()):
temp = "%s:%s" %(key,val)
#method 5
print 'span time 5 :%s' %str(time.time()-startTime) testDict()

多次实验的平均值:

span time 1 :0.206
span time 2 :0.508
span time 3 :0.195
span time 4 :0.205
span time 5 :0.307

python 中 五种字典(dict)的遍历方法,实验法比较性能。的更多相关文章

  1. 二、python 中五种常用的数据类型

    一.字符串 单引号定义: str1 = 'hello' 双引号定义: str1 = "hello" 三引号定义:""" 人生苦短, 我用python! ...

  2. Python中常见的字典dict处理

    #字典的赋值d = [{"dasda": 123, "gsgsg": 3344}, {"dasdz": 123, "gsksg&q ...

  3. Python中3种内建数据结构:列表、元组和字典

    Python中3种内建数据结构:列表.元组和字典 Python中有3种内建的数据结构:列表.元组和字典.参考简明Python教程 1. 列表 list是处理一组有序项目的数据结构,即你可以在一个列表中 ...

  4. python调用数据返回字典dict数据的现象2

    python调用数据返回字典dict数据的现象2 思考: 话题1连接:https://www.cnblogs.com/zwgbk/p/10248479.html在打印和添加时候加上内存地址id(),可 ...

  5. python调用数据返回字典dict数据的现象1

    python调用数据返回字典dict数据的现象1 思考: 可以看到这两种情况,区别在于构造函数make()里赋值给字典dict的方式不同.使用相同的调用方式,而结果却完全不同.可以看到第二种情况才是我 ...

  6. Python中的list,tuple,dict和set

    List list的创建与检索 Python内置的一种数据类型是列表:list.list是一种有序的集合,可以随时添加和删除其中的元素. 构造list非常简单,直接用 [ ] 把list的所有元素都括 ...

  7. Python 中当前位置以及目录文件遍历操作

    Python 中当前位置以及目录文件遍历操作 当前位置 print(os.path.dirname(__file__)) 其中 dirname 会选择目录(文件夹),"__file__&qu ...

  8. python中readline判断文件读取结束的方法

    注:内容来自网络 本文实例讲述了python中readline判断文件读取结束的方法.分享给大家供大家参考.具体分析如下: 大家知道,python中按行读取文件可以使用readline函数,下面现介绍 ...

  9. 五种WordPress防止垃圾评论方法-过滤垃圾评论提高WP运行效率

    WordPress貌似和垃圾评论是一对“孪生兄弟”,无论在国内还是国外的空间主机上搭建的Wordpress博客,无论Wordpress有多少流量多么低的权重,垃圾评论都会自动找上门来,假如有好几天没有 ...

随机推荐

  1. Android调用相册拍照控件实现系统控件缩放切割图片

    android 下如果做处理图片的软件 可以调用系统的控件 实现缩放切割图片 非常好的效果 今天写了一个demo分享给大家 package cn.m15.test; import java.io.By ...

  2. (C语言)结构体成员的引用->(箭头)和 .(点)

    关于结构体成员的引用有这样的规律: 箭头(->):左边必须为指针: 点号(.):左边必须为实体. 那么如果一个结构体指针引用一个成员,这个成员又是一个结构体(并且是一个实体),那么如果要引用这个 ...

  3. 使用HelloCharts绘制柱状图

    首先下载依赖库 ,有现成的jar包:hellocharts-library-1.5.8.jar 在需要的布局中直接使用: <lecho.lib.hellocharts.view.ColumnCh ...

  4. BackTrack5-r3硬盘安装

    用Universal-USB-Installer-1.9.4.2这个工具,选择系统类型为Backtrack5 R3,选择Backtrack5 R3系统的ISO文件,然后选中U盘盘符,点击”Create ...

  5. 【转】Lua coroutine 不一样的多线程编程思路

    Lua coroutine 不一样的多线程编程思路 Sunday, Apr 26th, 2009 by Tim | Tags: coroutine, Lua 上周末开始看<Lua程序设计> ...

  6. java web项目中 获取resource路径下的文件路径

    public GetResource{ String path = GetResource.class.getClassLoader().getResource("xx/xx.txt&quo ...

  7. HDU 1548 A strange lift (bfs / 最短路)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1548 A strange lift Time Limit: 2000/1000 MS (Java/Ot ...

  8. Spring触发器配置Quartz

    参考文献:http://blog.csdn.net/liaq325/article/details/8269439 http://sundoctor.iteye.com/blog/441951 业务类 ...

  9. 我用工具怎么连接不上mysql数据库的? MySql access denied for user错误

    MySql access denied for user错误 方法/步骤   MySql远程连接时的"access denied for user **@**"错误,搞的我很头大, ...

  10. 在Bootstrap中 强调相关的类

    .text-muted:提示,使用浅灰色(#999) .text-primary:主要,使用蓝色(#428bca) .text-success:成功,使用浅绿色(#3c763d) .text-info ...