2025年3月31日 星期一 乙巳(蛇)年 正月初一 设为首页 加入收藏
rss
您当前的位置:首页 > 计算机 > 编程开发 > Python

python 字典遍历方法性能对比

时间:10-26来源:作者:点击数:58

最近项目中使用到了dict的遍历,笔者写了几年的python,大多数都是使用dict.keys()的遍历方式。无奈项目执行过程中当dict中的元素上千万的时候,两层for循环性能实在是扛不住,于是测试了一下几种遍历方法的性能

  • import timeit
  • DICT_SIZE = 5000
  • testDict = dict()
  • for i in range(DICT_SIZE):
  •     testDict[i] = i 
  • assert len(testDict) == DICT_SIZE
  • def test1():
  •      for k in testDict.keys():
  •         key = k 
  •         value = testDict[k]
  • def test2():
  •     for k in testDict:
  •         key = k 
  •         value = testDict[k]
  • def test3():
  •     for k in testDict.iterkeys():
  •         key = k 
  •         value = testDict[k]
  • def test4():
  •     for k,v in testDict.items():
  •         key = k 
  •         value = v 
  • 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)
  • print timeit.timeit("test4()", setup="from __main__ import test4", number=2000)

测试结果如下:

0.698087930679
0.643119812012
0.642452955246
0.88481593132
方便获取更多学习、工作、生活信息请关注本站微信公众号城东书院 微信服务号城东书院 微信订阅号
推荐内容
相关内容
栏目更新
栏目热门