d = {}
for i in range(100000000):
d['%s'%i] = None
print len(d)#100000000
start_time = time.time()
print 'a' in d#False
print "in time consume: ", time.time() - start_time#in time consume: 0.0
start_time = time.time()
print d.get('a') #None
print "get: ", time.time() - start_time #get: 0.0
start_time = time.time()
print 'a' in d.keys()#False
print 'd.keys time consume: ', time.time() - start_time#d.keys time consume: 9.39199995995
测试结论:
1、使用字典的in操作查找的时间复杂度为O(1)
2、get操作的时间负责度为O(1)
字典for in 性能测试
最新推荐文章于 2020-11-23 21:35:42 发布