I'm using a dict
to store cached paths in a filesystem I've implemented. The keys in the dict
are all strings. After some profiling I've demonstrated that lookups in the dictionary are a bottleneck. My understanding is that dict
is highly optimized. Unfortunately the 开发者_JAVA百科precision of cProfile does not include the inner workings of the dict lookup code.
Is there some reason that lookups for strings would be so slow in Python 3? Is there something I can do to workaround or improve the performance?
I think, there is a necessity to evaluate the source code to understand what makes the difference. Recently i've tried to port Python2 code to Python3, come up with same poor lookup times, Python2 is ~30% faster than Python3. This is not actual code but a dummy class to figure out where the problem actually is. Sample code:
"""
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)
精彩评论