python 中 五种字典(dict)的遍历方法,实验法比较性能。
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)的遍历方法,实验法比较性能。的更多相关文章
- 二、python 中五种常用的数据类型
一.字符串 单引号定义: str1 = 'hello' 双引号定义: str1 = "hello" 三引号定义:""" 人生苦短, 我用python! ...
- Python中常见的字典dict处理
#字典的赋值d = [{"dasda": 123, "gsgsg": 3344}, {"dasdz": 123, "gsksg&q ...
- Python中3种内建数据结构:列表、元组和字典
Python中3种内建数据结构:列表.元组和字典 Python中有3种内建的数据结构:列表.元组和字典.参考简明Python教程 1. 列表 list是处理一组有序项目的数据结构,即你可以在一个列表中 ...
- python调用数据返回字典dict数据的现象2
python调用数据返回字典dict数据的现象2 思考: 话题1连接:https://www.cnblogs.com/zwgbk/p/10248479.html在打印和添加时候加上内存地址id(),可 ...
- python调用数据返回字典dict数据的现象1
python调用数据返回字典dict数据的现象1 思考: 可以看到这两种情况,区别在于构造函数make()里赋值给字典dict的方式不同.使用相同的调用方式,而结果却完全不同.可以看到第二种情况才是我 ...
- Python中的list,tuple,dict和set
List list的创建与检索 Python内置的一种数据类型是列表:list.list是一种有序的集合,可以随时添加和删除其中的元素. 构造list非常简单,直接用 [ ] 把list的所有元素都括 ...
- Python 中当前位置以及目录文件遍历操作
Python 中当前位置以及目录文件遍历操作 当前位置 print(os.path.dirname(__file__)) 其中 dirname 会选择目录(文件夹),"__file__&qu ...
- python中readline判断文件读取结束的方法
注:内容来自网络 本文实例讲述了python中readline判断文件读取结束的方法.分享给大家供大家参考.具体分析如下: 大家知道,python中按行读取文件可以使用readline函数,下面现介绍 ...
- 五种WordPress防止垃圾评论方法-过滤垃圾评论提高WP运行效率
WordPress貌似和垃圾评论是一对“孪生兄弟”,无论在国内还是国外的空间主机上搭建的Wordpress博客,无论Wordpress有多少流量多么低的权重,垃圾评论都会自动找上门来,假如有好几天没有 ...
随机推荐
- Hadoop 2.2.0学习笔记20131210
伪分布式单节点安装执行pi失败: [root@server- ~]# ./bin/hadoop jar share/hadoop/mapreduce/hadoop-mapreduce-examples ...
- Invalid byte 3 of 3-byte UTF-8 sequence
用maven编译,tomcat启动时报错:IOException parsing XML document from class path resource [applicationContext.x ...
- SoapUI中如何传递cookie
import com.eviware.soapui.support.types.StringToStringMap //Get all the cookies in the response def ...
- Different types of keystore in Java Overview
Different types of keystore in Java JKS DKS JCEKS PKCS12 PKCS11 http://www.pixelstech.net/article/14 ...
- windows系统常用快捷键及其作用
使用windows快捷键,使得工作起来事半功倍,你都懂了么? win 7操作系统快捷键,其余的操作系统有部分不一致,但总体都差不多 win+D: 显示桌面/隐藏桌面 (再次按win+D,下同) (wi ...
- 如何垂直居中一个<img>?
<!doctype html><html> <head> <meta charset="UTF-8"> <meta name= ...
- day11_API第一天
1.Eclipse常用操作 1:安装 A:解压 B:复制到指定的目录(不要有中文,和其他非法字符) 2:使用 A:打开软件的界面 B:新建一个项目(工程) C:在 ...
- 十分钟了解分布式计算:Petuum
Petuum是一个机器学习专用分布式计算框架,本文介绍其架构,并基于文章 More Effective Distributed ML via a Stale Synchronous Parallel ...
- ajax data数据里面传一个json到后台的写法
$.ajax({ url:url+"/crm/contact", type:'PUT', ...
- springMVC 拦截器简单配置
在spring 3.0甚础上,起来越多的用到了注解,从前的拦截器在配置文件中需要这样配置 <beans...> ... <bean id="measurementInter ...