dict .__ getitem__表演用弦乐

时间:2011-06-08 00:58:55

标签: python string unicode dictionary python-3.x

我正在使用dict在我实现的文件系统中存储缓存路径。 dict中的键都是字符串。经过一些分析后,我已经证明字典中的查找是一个瓶颈。我的理解是dict高度优化。不幸的是,cProfile的精度不包括dict查找代码的内部工作原理。

是否有某些原因在Python 3中查找字符串会如此之慢? 我可以做些什么来解决或提高性能?

1 个答案:

答案 0 :(得分:0)

我认为,有必要对源代码进行评估,以了解产生差异的原因。最近我试图将Python2代码移植到Python3,得出相同的查找次数,Python2比Python3快30%。这不是实际的代码,而是一个虚拟类来确定问题的实际位置。示例代码:

"""
python3
"""
from time import time
from random import randint

class Wooaah:

    def __init__(self):
        self.length=100000
        self.a=dict()
        self.b=dict()
        self.count=int()
        self.s=float()
        self.e=float()

    def dummy_data(self):
        for i in range(self.length):
            self.a[i]=time()
            self.b[i]=time()

    def setod(self):
        self.s=time()
        for i in self.a:
            if self.b.get(i):
                self.count+=1
        self.e=time()

a=Wooaah()
a.dummy_data()
a.setod()
print(a.count)
print(a.e-a.s)
相关问题