Not not x 和 Bool(x) 用哪個(gè)比較好?
今天來(lái)做一個(gè)選擇,就是 not not x 和 bool(x) 用哪個(gè)比較好?
他們都可以把 x 變成一個(gè)布爾類型的值:
- >>> x = 123
- >>> not not x
- True
- >>> bool(x)
- True
- >>>
那么誰(shuí)更快呢?我們寫段代碼,跑個(gè) 100 萬(wàn)次,來(lái)比較下誰(shuí)更快:
- import timeit
- def bool_convert(x):
- return bool(x)
- def notnot_convert(x):
- return not not x
- def main():
- trials = 10_000_000
- kwargs = {
- "setup": "x=42",
- "globals": globals(),
- "number": trials,
- }
- notnot_time = timeit.timeit("notnot_convert(x)", **kwargs)
- bool_time = timeit.timeit("bool_convert(x)", **kwargs)
- print(f"{bool_time = :.04f}")
- print(f"{notnot_time = :.04f}")
- if __name__ == "__main__":
- main()
運(yùn)行結(jié)果如下:
其實(shí) bool(x) 慢的原因在于它是一個(gè)函數(shù)調(diào)用,而 not not x 就是一條指令,具有更快捷的轉(zhuǎn)換為布爾值的路徑,這一點(diǎn)可以從字節(jié)碼可以看出來(lái):
bool(x) 多了 LOAD_GLOBAL 和 CALL_FUNCTION。
這里附一下相關(guān)字節(jié)碼的官方說(shuō)明:
- LOAD_GLOBAL(namei)
- Loads the global named co_names[namei] onto the stack.
- CALL_FUNCTION(argc)
- Calls a callable object with positional arguments. argc indicates the number of positional arguments. The top of the stack contains positional arguments, with the right-most argument on top. Below the arguments is a callable object to call. CALL_FUNCTION pops all arguments and the callable object off the stack, calls the callable object with those arguments, and pushes the return value returned by the callable object.
- UNARY_NOT
- Implements TOS = not TOS.
最后
從結(jié)果來(lái)看,not not x 比 bool(x) 更快,主要原因在于 bool(x) 是一個(gè)函數(shù)調(diào)用,函數(shù)調(diào)用需要參數(shù)壓入棧頂,堆棧的頂部包含位置參數(shù),最右邊的參數(shù)在頂部,參數(shù)下面是要調(diào)用的可調(diào)用對(duì)象。CALL_FUNCTION 從堆棧中彈出所有參數(shù)和可調(diào)用對(duì)象,使用這些參數(shù)調(diào)用可調(diào)用對(duì)象,并推送可調(diào)用對(duì)象返回的返回值,這一過(guò)程比一個(gè) not 指令要慢得多。
不過(guò)我仍然推薦你使用 bool(x),因?yàn)樗目勺x性更高,而且,你也不太可能調(diào)用它 100萬(wàn)次。























