python dict / list comprehension:为什么它比for循环慢?

时间:2015-12-04 01:07:38

标签: python

考虑events这里有大约48,000个字典对象:

keyed_events = { gid: [ r for r in events if r['gid'] == gid ] for gid in gidlist }

比以下慢4倍:

keyed_events = {}
for event in events:
    gid = event['gid']
    if gid not in keyed_events:
        keyed_events[gid] = []
    keyed_events[gid].append(event)

虽然前者看起来更有效率。它为什么慢?通过events迭代dict理解的每一遍?

3 个答案:

答案 0 :(得分:4)

keyed_events = { gid: [ r for r in events if r['gid'] == gid ] for gid in gidlist }

列表/字典理解在len(gidlist)*len(events)的循环中循环events时会运行gidlist次。

另一方面,for循环在events上只有一个for循环,gid not in keyed_eventsO(1)操作

答案 1 :(得分:3)

您的词典+列表理解与此代码更匹配:

keyed_events = {}
for gid in gidlist:
    for r in events:
        if r['gid'] == gid:
            keyed_events[gid].append(r)

请注意,循环是双重嵌套。你循环遍历每个gid的所有事件。

与您的迭代代码更接近的匹配是:

keyed_events = itertools.groupby(events, 'gid')

答案 2 :(得分:0)

我认为这是因为在代码段中您反复遍历gidlistevents,但是在第一种方法中,只是迭代events

完全我认为内联for效率低下