115

if-elif-elif-else ステートメントがあり、99% の確率で else ステートメントが実行されます。

if something == 'this':
    doThis()
elif something == 'that':
    doThat()
elif something == 'there':
    doThere()
else:
    doThisMostOfTheTime()

このコンストラクトは何度も実行されますが、他の条件に到達する前にすべての条件を調べるため、Pythonic は言うまでもなく、これはあまり効率的ではないと感じています一方、これらの条件のいずれかが満たされているかどうかを知る必要があるため、とにかくテストする必要があります。

これをより効率的に行うことができるかどうか、またその方法を知っている人はいますか?

4

9 に答える 9

116

コード...

options.get(something, doThisMostOfTheTime)()

...高速であるように見えますが、実際にはif... elif...elseコンストラクトよりも低速です。これは、関数を呼び出す必要があり、タイトなループではパフォーマンスのオーバーヘッドが大きくなる可能性があるためです。

これらの例を考えてみましょう...

1.py

something = 'something'

for i in xrange(1000000):
    if something == 'this':
        the_thing = 1
    elif something == 'that':
        the_thing = 2
    elif something == 'there':
        the_thing = 3
    else:
        the_thing = 4

2.py

something = 'something'
options = {'this': 1, 'that': 2, 'there': 3}

for i in xrange(1000000):
    the_thing = options.get(something, 4)

3.py

something = 'something'
options = {'this': 1, 'that': 2, 'there': 3}

for i in xrange(1000000):
    if something in options:
        the_thing = options[something]
    else:
        the_thing = 4

4.py

from collections import defaultdict

something = 'something'
options = defaultdict(lambda: 4, {'this': 1, 'that': 2, 'there': 3})

for i in xrange(1000000):
    the_thing = options[something]

...そして、彼らが使用する CPU 時間の量に注意してください...

1.py: 160ms
2.py: 170ms
3.py: 110ms
4.py: 100ms

... からのユーザー時間を使用しますtime(1)

オプション #4 には、個別のキー ミスごとに新しいアイテムを追加する追加のメモリ オーバーヘッドがあります。オリジナルのコンストラクト。

于 2013-06-18T10:29:24.827 に答える
84

私は辞書を作成します:

options = {'this': doThis,'that' :doThat, 'there':doThere}

今すぐ使用してください:

options.get(something, doThisMostOfTheTime)()

辞書にsomethingが見つからない場合は、デフォルト値を返しますoptionsdict.getdoThisMostOfTheTime

いくつかのタイミング比較:

脚本:

from random import shuffle
def doThis():pass
def doThat():pass
def doThere():pass
def doSomethingElse():pass
options = {'this':doThis, 'that':doThat, 'there':doThere}
lis = range(10**4) + options.keys()*100
shuffle(lis)

def get():
    for x in lis:
        options.get(x, doSomethingElse)()

def key_in_dic():
    for x in lis:
        if x in options:
            options[x]()
        else:
            doSomethingElse()

def if_else():
    for x in lis:
        if x == 'this':
            doThis()
        elif x == 'that':
            doThat()
        elif x == 'there':
            doThere()
        else:
            doSomethingElse()

結果:

>>> from so import *
>>> %timeit get()
100 loops, best of 3: 5.06 ms per loop
>>> %timeit key_in_dic()
100 loops, best of 3: 3.55 ms per loop
>>> %timeit if_else()
100 loops, best of 3: 6.42 ms per loop

10**5存在しないキーと 100 個の有効なキーの場合::

>>> %timeit get()
10 loops, best of 3: 84.4 ms per loop
>>> %timeit key_in_dic()
10 loops, best of 3: 50.4 ms per loop
>>> %timeit if_else()
10 loops, best of 3: 104 ms per loop

したがって、通常の辞書では、キーを使用してチェックするのkey in optionsが最も効率的な方法です。

if key in options:
   options[key]()
else:
   doSomethingElse()
于 2013-06-18T10:07:53.850 に答える
9

ピピは使えますか?

元のコードを保持しながら pypy で実行すると、50 倍高速化されます。

CPython:

matt$ python
Python 2.6.8 (unknown, Nov 26 2012, 10:25:03)
[GCC 4.2.1 Compatible Apple Clang 3.0 (tags/Apple/clang-211.12)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>>
>>> from timeit import timeit
>>> timeit("""
... if something == 'this': pass
... elif something == 'that': pass
... elif something == 'there': pass
... else: pass
... """, "something='foo'", number=10000000)
1.728302001953125

ピピー:

matt$ pypy
Python 2.7.3 (daf4a1b651e0, Dec 07 2012, 23:00:16)
[PyPy 2.0.0-beta1 with GCC 4.2.1] on darwin
Type "help", "copyright", "credits" or "license" for more information.
And now for something completely different: ``a 10th of forever is 1h45''
>>>>
>>>> from timeit import timeit
>>>> timeit("""
.... if something == 'this': pass
.... elif something == 'that': pass
.... elif something == 'there': pass
.... else: pass
.... """, "something='foo'", number=10000000)
0.03306388854980469
于 2013-06-18T16:02:41.513 に答える