Today, i was curious to know which method will search a key very fast in dictionary. So i wrote small script which gives me time of execution of some methods.
A programmer can really take advantage to optimize their programe using this script.
import os
import sys
import time
__doc__="""
benchmark script for dict method
"""
a=range(10000)
b=range(9000,10000)
c=dict(zip(a,b))
t1 = time.time()
k = c.keys()
if 98 in k:
t2 = time.time()
print 'if 98 in c.keys() :',(t2-t1)*1000
t1 = time.time()
if c.has_key(98):
t2 = time.time()
print 'c.has_key(98) :',(t2-t1)*1000
t1 = time.time()
if 98 in c:
t2 = time.time()
print 'if 98 in c: ',(t2-t1)*1000
No comments:
Post a Comment