闲话少叙,python中经常会对字典的索引进行搜索判断,如判断‘user’是否为{'user1':'New Student','user2':'Old student'}的索引,本文总结了5种方法进行索引的搜索,并比较了运行时间。
五种方法有:
① index in dict.keys()
② dict.has_key(index)
③ index in dict
④ index in set(dict)
⑤ index in set(dict.keys())
先构建一个程序运行时间的函数,用于测试。
from time import clock as now
def costTime(f, testDict, num, describe):
start = now()
f(testDict, num)
finish = now()
return 'The costing time of %s is %f' % (describe, finish - start)
然后分别写出五个函数:
#测试dict.keys()
def test_dictKeys(testDict, num):
for i in range(num):
if i in testDict.keys():
testDict[i]
#测试dict.has_key()
def test_has_key(testDict, num):
for i in range(num):
if testDict.has_key(i):
testDict[i]
#测试dict
def test_dict(testDict, num):
for i in range(num):
if i in testDict:
testDict[i]
#测试set
def test_set(testDict, num):
tt = set(testDict)
for i in range(num):
if i in tt:
testDict[i]
#测试set_dictkeys
def test_setDictKeys(testDict, num):
tt = set(testDict.keys())
for i in range(num):
if i in tt:
testDict[i]
下面为主程序
num = 50000
#初始化字典
test = dict()
for i in range(num):
test[i] = i-0.1
print costTime(test_dictKeys, test, num, 'dict.keys()')
print costTime(test_has_key, test, num, 'dict.has_key()')
print costTime(test_dict, test, num, 'in dict')
print costTime(test_set, test, num, 'in set')
print costTime(test_setDictKeys, test, num, 'set(dict.keys())')
得到的结果为:
The costing time of dict.keys() is 79.946342
The costing time of dict.has_key() is 0.017616
The costing time of in dict is 0.012359
The costing time of in set is 0.015817
The costing time of set(dict.keys()) is 0.016689
从以上运行时间上,可以看出第三种方法是最快速的,第一种方法是最慢的,而且相差不是一星半点。究其原因,应该是dict.keys()得到的结果是有序 的list数据类型,而最后面三种是dict或者set类型,是无序的。但更深层次的解释,还需要继续深究。也请大神们赐教。
五种方法有:
① index in dict.keys()
② dict.has_key(index)
③ index in dict
④ index in set(dict)
⑤ index in set(dict.keys())
先构建一个程序运行时间的函数,用于测试。
from time import clock as now
def costTime(f, testDict, num, describe):
start = now()
f(testDict, num)
finish = now()
return 'The costing time of %s is %f' % (describe, finish - start)
然后分别写出五个函数:
#测试dict.keys()
def test_dictKeys(testDict, num):
for i in range(num):
if i in testDict.keys():
testDict[i]
#测试dict.has_key()
def test_has_key(testDict, num):
for i in range(num):
if testDict.has_key(i):
testDict[i]
#测试dict
def test_dict(testDict, num):
for i in range(num):
if i in testDict:
testDict[i]
#测试set
def test_set(testDict, num):
tt = set(testDict)
for i in range(num):
if i in tt:
testDict[i]
#测试set_dictkeys
def test_setDictKeys(testDict, num):
tt = set(testDict.keys())
for i in range(num):
if i in tt:
testDict[i]
下面为主程序
num = 50000
#初始化字典
test = dict()
for i in range(num):
test[i] = i-0.1
print costTime(test_dictKeys, test, num, 'dict.keys()')
print costTime(test_has_key, test, num, 'dict.has_key()')
print costTime(test_dict, test, num, 'in dict')
print costTime(test_set, test, num, 'in set')
print costTime(test_setDictKeys, test, num, 'set(dict.keys())')
得到的结果为:
The costing time of dict.keys() is 79.946342
The costing time of dict.has_key() is 0.017616
The costing time of in dict is 0.012359
The costing time of in set is 0.015817
The costing time of set(dict.keys()) is 0.016689
从以上运行时间上,可以看出第三种方法是最快速的,第一种方法是最慢的,而且相差不是一星半点。究其原因,应该是dict.keys()得到的结果是有序 的list数据类型,而最后面三种是dict或者set类型,是无序的。但更深层次的解释,还需要继续深究。也请大神们赐教。