4
votes

I have wrote some very simple tests (I know, they are not 'conclusive', but they make me curious). I ran with optimization and all that jazz.

from time import time

alist = [ 2, 4, 6, 8, 10, 12, 24, 48, 64, 128 ]

def all_even( alist ):
    for val in alist:
        if not (val & 1) == 0:
            return False
    return True

def all_even_bad( alist ):
    result = False
    for val in alist:
        if not (val & 1) == 0:
            result = False
        else:
            result = True
    return result

def main():
    start = time()
    for i in range(1, 10000):
        all_even( alist )
    print('All even: {0}'.format(time() - start))

    start = time()
    for i in range(1, 10000):
        all_even_bad( alist )
    print('All even bad: {0}'.format(time() - start))


    start = time()
    for i in range(1, 10000):
        all( val & 1 == 0 for val in alist )
    print('All one: {0}'.format(time() - start))


if __name__ == '__main__':
    main()

I get results around:

> All even: 2.86299991608 
> All even bad: 3.71399998665 
> All one: 3.89900016785

It appears the built in function doesn't bail out early?

3
There is definitely something fishy going on here. You're only looping over a list of 10 items 10000 times. Your times should not be on the order of seconds. Also note that your all_even_bad function is completely wrong ;^) (It really is only checking the last element). - mgilson
Sorry about that-- I had a whole bunch of tests but didn't think to post all of the code. I tried to post a small but runnable example. :) - user427390

3 Answers

7
votes

all() definitely does exit early, I think the behavior difference is just the result of the overhead required to create the generator.

Here is some proof that all() does exit early:

In [8]: alist = [3] + [0] * 2**20    # alist bigger, with an early odd

In [10]: %timeit all_even(alist)
1000000 loops, best of 3: 309 ns per loop

In [11]: %timeit all_even_bad(alist)
10 loops, best of 3: 133 ms per loop

In [12]: %timeit all(val & 1 == 0 for val in alist)
1000000 loops, best of 3: 891 ns per loop

Note that even though all() is slower than all_even() here, it is still significantly faster than the version of the function that doesn't exit early.

3
votes

You have no elements that fail the test, therefore there is no way for it to short circuit.

1
votes

Since all of the numbers in your list are in fact even, it could not logically bail out early? The overhead of your all() call might come from the construction of a generator object.