简体   繁体   中英

What's the time complexity of Python built-in Function `all()`

How to know the time complexity of Python built-in Function all() ?

I have tried these ways but found no answer.

  1. searched on official complexity documentation, but found no answer.
  2. Tried to find in Python source code. But I cannot find where the algorithm all() . This is the only file about all() I found. enter image description here

Definitionally O(n) . It has to check the truthiness of all the values provided until it finds a falsy value. It short-circuits (if it finds a single falsy value, it stops immediately and returns False without checking the rest of the input), so in many cases it won't do all the work, but if the input is in fact all truthy, it must, by definition, check all of them, doing O(n) work. Big-O doesn't care about the possibility of short-circuiting.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM