Why is “not” faster than “bool()” in Python (or, speed of Python functions vs. statements)?

后端 未结 2 1060
北恋
北恋 2021-01-11 19:41

I made an interesting observation the other day. I was experimenting with various ways to get the \"truthiness\" of an object and the speed of each, and I noticed that

2条回答
  •  走了就别回头了
    2021-01-11 19:59

    All function calls have significant overhead -- you're creating a new stack frame to hold the new locals you have within your call, after all.

    If this were a more expensive operation, that overhead would get lost in the noise. Since you're looking at such a trivial operation, it stands out.

提交回复
热议问题