我用timeit获得了非常令人惊讶的结果,有人可以告诉我,如果我做错了吗?我使用的是Python 2.7.
这是文件speedtest_init.py的内容:
import random
to_count = [random.randint(0, 100) for r in range(60)]
Run Code Online (Sandbox Code Playgroud)
这些是speedtest.py的内容:
__author__ = 'BlueTrin'
import timeit
def test_init1():
print(timeit.timeit('import speedtest_init'))
def test_counter1():
s = """\
d = defaultdict(int);
for i in speedtest_init.to_count:
d[i] += 1
"""
print(timeit.timeit(s, 'from collections import defaultdict; import speedtest_init;'))
def test_counter2():
print(timeit.timeit('d = Counter(speedtest_init.to_count);', 'from collections import Counter; import speedtest_init;'))
if __name__ == "__main__":
test_init1()
test_counter1()
test_counter2()
Run Code Online (Sandbox Code Playgroud)
控制台输出是:
C:\Python27\python.exe C:/Dev/codility/chlorum2014/speedtest.py
2.71501962931
65.7090444503
91.2953839048
Process finished with exit code 0
Run Code Online (Sandbox Code Playgroud)
我认为默认情况下timeit()运行代码的1000000倍,所以我需要将时间除以1000000,但令人惊讶的是Counter慢于defaultdict().
这是预期的吗?
编辑:
使用dict也比defaultdict(int)更快: …
我想编写一些测试来分析python中不同操作的效率,即字典理解和dict生成器的比较。
为了验证这一点,我想我会尝试一个简单的示例:使用字典计算列表中的单词数。
现在,我知道您可以使用collections.Counter(按照这里的答案:如何计算Python中列表项的出现?)进行此操作,但是我的目标是测试内存性能。
一种“长手”方法是在基本循环中进行操作。
from pprint import pprint
# Read in some text to create example data
with open('text.txt') as f:
words = f.read().split()
dict1 = {}
for w in words:
if not dict1.get(w):
dict1[w] = 1
else:
dict1[w] += 1
pprint(dict1)
Run Code Online (Sandbox Code Playgroud)
结果:
{'a': 62,
'aback': 1,
'able': 1,
'abolished': 2,
'about': 6,
'accept': 1,
'accepted': 1,
'accord': 1,
'according': 1,
'across': 1,
...
Run Code Online (Sandbox Code Playgroud)
然后,在字典理解中尝试执行相同操作时,我有些卡住了:
dict2 = { w: 1 if not dict2.get(w) else dict2.get(w) + …Run Code Online (Sandbox Code Playgroud) 我在python中有一本字典
d = {tags[0]: value, tags[1]: value, tags[2]: value, tags[3]: value, tags[4]: value}
Run Code Online (Sandbox Code Playgroud)
想象这个dict大10倍,它有50个键和50个值.可以在此标签中找到重复项,但即使这样,值也是必不可少的.我怎样才能简单地修剪它以重新获得新的字典而不需要复制键,而是使用值的总和?
d = {'cat': 5, 'dog': 9, 'cat': 4, 'parrot': 6, 'cat': 6}
结果
d = {'cat': 15, 'dog': 9, 'parrot': 6}
python ×3
counter ×1
defaultdict ×1
dictionary ×1
duplicates ×1
generator ×1
performance ×1
python-2.7 ×1
timeit ×1