In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces what are the differences between not not x and bool (x) in python, which can be used for reference by interested friends. I hope you can learn a lot after reading this article.
They can all change x to a Boolean value:
> x = 123 > not not xTrue > bool (x) True >
So who's faster? Let's write a piece of code and run a million times to compare who is faster:
Import timeitdef bool_convert (x): return bool (x) def notnot_convert (x): return notnot xdef main (): trials = 10000000000 kwargs = {"setup": "Xero42", "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 ()
The running results are as follows:
In fact, the reason why bool (x) is slow is that it is a function call, and not not x is an instruction with a faster path to a Boolean value, as can be seen from the bytecode:
Bool (x) has more LOAD_GLOBAL and CALL_FUNCTION.
Here is an official description of the relevant bytecode:
LOAD_GLOBAL (namei) Loads the global named co_ namesnamei 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_NOTImplements TOS = not TOS.
Finally:
As a result, not not x is faster than bool (x), mainly because bool (x) is a function call that requires parameters to be pushed into the top of the stack, the top of the stack contains position parameters, the rightmost parameter is at the top, and below the parameters is the callable object to be called. CALL_FUNCTION pops all parameters and callable objects from the stack, invokes callable objects with these parameters, and pushes the return value returned by callable objects, which is much slower than a not instruction.
However, I still recommend that you use bool (x) because it is more readable and we are unlikely to call it 1 million times.
Thank you for reading this article carefully. I hope the article "what is the difference between not not x and bool (x) in python" shared by the editor will be helpful to you. At the same time, I also hope you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.