setdefault vs defaultdict性能 [英] setdefault vs defaultdict performance
问题描述
我正在为性能很重要的应用程序编写代码.我想知道为什么defaultdict
似乎比setdefault
快.
I am writing code for an application where performance is important. I am wondering why defaultdict
seems to be faster then setdefault
.
我希望能够使用setdefault
,主要是因为我不喜欢嵌套的defaultdict
的打印输出(请参见下面的实现).
I would like to be able to use setdefault
, mostly because i do not like the print output of the nested defaultdict
(see implementation below).
在我的代码中,我需要测试element_id
是否已经是字典的键.
In my code, i need to test if element_id
is already a key of the dict.
这是我正在测试的两个功能:
Here are the two functions that i am testing:
def defaultdictfunc(subcases,other_ids,element_ids):
dict_name= defaultdict(lambda: defaultdict(lambda: defaultdict(dict)))
for subcase in subcases:
for other_id in other_ids:
for element_id in element_ids:
if element_id in dict_name[subcase][other_id]:
# error duplicate element_id
pass
else:
dict_name[subcase][other_id][element_id]=0
return dict_name
def setdefaultfunc(subcases,other_ids,element_ids):
dict_name={}
for subcase in subcases:
for other_id in other_ids:
for element_id in element_ids:
if element_id in dict_name.setdefault(subcase,{}).setdefault(other_id,{}):
# error duplicate element_id
pass
else:
dict_name[subcase][other_id][element_id]=0
return dict_name
IPython输入和输出:
IPython input and output:
In [1]: from numpy.random import randint
In [2]: subcases,other_ids,element_ids=(randint(0,100,100),randint(0,100,100),randint(0,100,100))
In [5]: from collections import defaultdict
In [6]: defaultdictfunc(subcases,other_ids,element_ids)==setdefaultfunc(subcases,other_ids,element_ids)
Out[6]: True
In [7]: %timeit defaultdictfunc(subcases,other_ids,element_ids)
10 loops, best of 3: 177 ms per loop
In [8]: % timeit setdefaultfunc(subcases,other_ids,element_ids)
1 loops, best of 3: 351 ms per loop
为什么setdefaultfunc
变慢.我认为基础代码将是相同的.有没有提高速度的方法?
Why is setdefaultfunc
slower. I thought the underlying code would be the same. Is there a way to improve its speed?
谢谢
推荐答案
根据用户 aneroid :
defaultdict
比dict.setdefault()
更快是有道理的,因为前者在创建时将整个字典设置为默认值,而setdefault()在读取时按元素进行设置.使用setdefault的一个原因是,您分配的默认值是基于键(或某物)的,而不是整个字典的通用默认值.
It would make sense that
defaultdict
is faster thatdict.setdefault()
since the former sets its default for the entire dict at creation time, whereas setdefault() does it per element when it is read. One reason to use setdefault is when the default you assign is based on the key (or something) rather than a generic default for the entire dict.
这篇关于setdefault vs defaultdict性能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!