第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

當(dāng)else做得最多的時(shí)候,最有效的方式來(lái)執(zhí)行if-elif-elif-else語(yǔ)句嗎?

當(dāng)else做得最多的時(shí)候,最有效的方式來(lái)執(zhí)行if-elif-elif-else語(yǔ)句嗎?

動(dòng)漫人物 2021-03-28 10:42:18
我有一條if-elif-elif-else語(yǔ)句,其中99%的時(shí)間執(zhí)行else語(yǔ)句:if something == 'this':    doThis()elif something == 'that':    doThat()elif something == 'there':    doThere()else:    doThisMostOfTheTime()這個(gè)構(gòu)造完成了很多,但是由于它在碰到其他情況之前會(huì)遍歷所有條件,所以我覺(jué)得這不是很有效,更不用說(shuō)Pythonic了。另一方面,它確實(shí)需要知道是否滿足任何這些條件,因此無(wú)論如何都應(yīng)該對(duì)其進(jìn)行測(cè)試。是否有人知道是否以及如何才能更有效地完成此工作,或者這僅僅是做到這一點(diǎn)的最佳方法?
查看完整描述

3 回答

?
斯蒂芬大帝

TA貢獻(xiàn)1827條經(jīng)驗(yàn) 獲得超8個(gè)贊

你可以使用pypy嗎?


保留原始代碼,但在pypy上運(yùn)行可使我的速度提高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

pypy:


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


查看完整回答
反對(duì) 回復(fù) 2021-04-02
  • 3 回答
  • 0 關(guān)注
  • 415 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)