我需要使用大数据结构,更具体地说,是一个大字典来完成查找工作。
一开始我的代码是这样的:
#build the dictionary
blablabla
#look up some information in the ditionary
blablabla
由于我需要多次查找,我开始意识到将它实现为一个函数是一个好主意,比如lookup(info)。
那么问题来了,大字典该怎么处理呢?
我应该使用lookup(info, dictionary)将其作为参数传递,还是应该在main()中初始化字典并将其用作全局变量?
第一个似乎更优雅,因为我认为维护全局变量很麻烦。但另一方面,我不确定将大字典传递给函数的效率。它会被多次调用,如果参数传递效率低下,那肯定是一场噩梦。
谢谢。
编辑1:
我只是对以上两种方式做了一个实验:
这是代码片段。lookup1实现参数传递查找,而 lookup2 使用全局数据结构“big_dict”。
class CityDict():
def __init__():
self.code_dict = get_code_dict()
def get_city(city):
try:
return self.code_dict[city]
except Exception:
return None
def get_code_dict():
# initiate code dictionary from file
return code_dict
def lookup1(city, city_code_dict):
try:
return city_code_dict[city]
except Exception:
return None
def lookup2(city):
try:
return big_dict[city]
except Exception:
return None
t = time.time()
d = get_code_dict()
for i in range(0, 1000000):
lookup1(random.randint(0, 10000), d)
print "lookup1 is %f" % (time.time() - t)
t = time.time()
big_dict = get_code_dict()
for i in range(0, 1000000):
lookup2(random.randint(0, 1000))
print "lookup2 is %f" % (time.time() - t)
t = time.time()
cd = CityDict()
for i in range(0, 1000000):
cd.get_city(str(i))
print "class is %f" % (time.time() - t)
这是输出:
lookup1 是8.410885
lookup2 是8.157661
类是4.525721
所以看起来这两种方式几乎是一样的,而且是的,全局变量的方式效率更高一点。
编辑2:
添加了Amber建议的class版本,然后再次测试效率。然后我们可以从结果中看出 Amber 是对的,我们应该使用类版本。