python dict 遍历key效率问题
来源:互联网 发布:青岛知行天下 编辑:程序博客网 时间:2024/06/03 13:03
如题
#!/usr/bin/env python# encoding: utf-8import timeitDICT_SIZE = 100*10000testDict = dict()for i in range(DICT_SIZE): testDict[i] = i # 构建大小为100W的字典assert len(testDict) == DICT_SIZEdef test1(): for _ in testDict.keys(): passdef test2(): for _ in testDict: passdef test3(): for _ in testDict.iterkeys(): pass# 分别测试2K次print timeit.timeit("test1()", setup="from __main__ import test1", number=2000) print timeit.timeit("test2()", setup="from __main__ import test2", number=2000)print timeit.timeit("test3()", setup="from __main__ import test3", number=2000)output-------------->41.570110969233.331320587433.1459584286
之前一直以为keys是最快的,后来发现查找一个key的时候居然还没有has_key()快,后来在网上查看发现了这个,感觉有点收获了。python3取消了has_key以后就可以用for _ in testDict:这个方式来进行索引了。
1 0
- python dict 遍历key效率问题
- python dict reg key
- python dict 数据结构遍历
- Python 读Dict数据的方法,解决key 不在dict的问题,get()函数,setdefault()函数
- python dict 按key排序
- Python的Dict版图遍历
- python dict.get()和dict['key']的使用
- 【python】判断key是否存在于dict
- Python字典key值查询效率低的问题
- python嵌套dict的深度遍历合并
- python中遍历dict的value
- python list和dict的遍历
- python 字典(dict)用for遍历
- 【详解】Python处理大量数据与DICT遍历的优化问题
- 遍历dict
- python--用户自定义类对象作为dict的key
- python dict order by key 字典关键字排序
- python字符编码导致dict中key不同
- 面试官在面试时让我去破解一个软件,我成功了
- ubuntu12.04安装tftp、配置tftp服务 错误解决方案
- python 数据类型String类型总结(二(2))
- 骑士
- MongoDB 索引
- python dict 遍历key效率问题
- 蓝牙聊天室App设计与实现
- 搭建http请求方式的git
- Android Bitmap 缓存策略
- AndroidStudio 混淆打包
- play_scala框架学习07 静态文件
- 循序渐进Java Socket网络编程(多客户端、信息共享、文件传输)
- Codeforces Round #360 (Div. 2) D. Remainders Game(数学)
- Android ASDL进程通信