Not not x 和 Bool(x) 用哪個比較好?
今天來做一個選擇,就是 not not x 和 bool(x) 用哪個比較好?
他們都可以把 x 變成一個布爾類型的值:
- >>> x = 123
- >>> not not x
- True
- >>> bool(x)
- True
- >>>
那么誰更快呢?我們寫段代碼,跑個 100 萬次,來比較下誰更快:
- 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) 慢的原因在于它是一個函數(shù)調(diào)用,而 not not x 就是一條指令,具有更快捷的轉(zhuǎn)換為布爾值的路徑,這一點(diǎn)可以從字節(jié)碼可以看出來:
bool(x) 多了 LOAD_GLOBAL 和 CALL_FUNCTION。
這里附一下相關(guān)字節(jié)碼的官方說明:
- 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é)果來看,not not x 比 bool(x) 更快,主要原因在于 bool(x) 是一個函數(shù)調(diào)用,函數(shù)調(diào)用需要參數(shù)壓入棧頂,堆棧的頂部包含位置參數(shù),最右邊的參數(shù)在頂部,參數(shù)下面是要調(diào)用的可調(diào)用對象。CALL_FUNCTION 從堆棧中彈出所有參數(shù)和可調(diào)用對象,使用這些參數(shù)調(diào)用可調(diào)用對象,并推送可調(diào)用對象返回的返回值,這一過程比一個 not 指令要慢得多。
不過我仍然推薦你使用 bool(x),因?yàn)樗目勺x性更高,而且,你也不太可能調(diào)用它 100萬次。