Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8278293
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T08:57:43+00:00 2026-06-08T08:57:43+00:00

I have wrote some very simple tests (I know, they are not ‘conclusive’, but

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T08:57:44+00:00Added an answer on June 8, 2026 at 8:57 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm creating some tests with JMeter, the situation is very simple, I have a
i wrote a very simple Aspect with Spring AOP. It works, but i have
I have a graph representing users and some articles they wrote. I need to
I have wrote some code to find out of 3 variables witch is the
I have wrote a script to do some backup. There is a if block
I have been refactoring throwaway code which I wrote some years ago in a
I have been reading about Scala for a while and even wrote some small
I have heard that prepared statements with SQLite should improve performance. I wrote some
I have this header file, zeeheader.h, and I wrote some classes in it, I'm
I have some python code that I wrote to convert a python list into

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.