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 7786065
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T20:21:23+00:00 2026-06-01T20:21:23+00:00

Okay, so I probably shouldn’t be worrying about this anyway, but I’ve got some

  • 0

Okay, so I probably shouldn’t be worrying about this anyway, but I’ve got some code that is meant to pass a (possibly very long, possibly very short) list of possibilities through a set of filters and maps and other things, and I want to know if my implementation will perform well.

As an example of the type of thing I want to do, consider this chain of operations:

  • get all numbers from 1 to 100
  • keep only the even ones
  • square each number
  • generate all pairs [i, j] with i in the list above and j in [1, 2, 3, 4,5]
  • keep only the pairs where i + j > 40

Now, after doing all this nonsense, I want to look through this set of pairs [i, j] for a pair which satisfies a certain condition. Usually, the solution is one of the first entries, in which case I don’t even look at any of the others. Sometimes, however, I have to consume the entire list, and I don’t find the answer and have to throw an error.

I want to implement my “chain of operations” as a sequence of generators, i.e., each operation iterates through the items generated by the previous generator and “yields” its own output item by item (a la SICP streams). That way, if I never look at the last 300 entries of the output, they don’t even get processed. I known that itertools provides things like imap and ifilter for doing many of the types of operations I would want to perform.

My question is: will a series of nested generators be a major performance hit in the cases where I do have to iterate through all possibilities?

  • 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-01T20:21:24+00:00Added an answer on June 1, 2026 at 8:21 pm

    I tried two implementations, one using generators and one without generators. I tested it in 2.7 so range returns a list rather than an iterator.

    Here is the implementations

    Using Generators

    def foo1():
        data = ((a,b) for a in (i*i for i in xrange(1,101) if i%2) for b in [1,2,3,4,5] if a+b > 40)
        return list(data)
    

    Without Generators

    def foo2():
        result=[]
        for i in range(1,101):
            if i%2:
                i=i*i
                for j in [1,2,3,4,5]:
                    if i+j > 40:
                        result+=[(i,j)]
        return result
    

    Mixing Both so as not to append a list

    def foo3():
        data=[(a,b) for a in (i*i for i in range(1,101)) for b in [1,2,3,4,5] if a+b > 40] 
        return data
    

    Creating Temporary Lists

    def foo4():
        data=[(a,b) for a in [i*i for i in range(1,101)] for b in [1,2,3,4,5] if a+b > 40]
        return data
    

    Here are my results

    >>> t1=timeit.Timer("foo1()","from __main__ import foo1")
    >>> t2=timeit.Timer("foo2()","from __main__ import foo2")
    >>> t3=timeit.Timer("foo3()","from __main__ import foo3")
    >>> t4=timeit.Timer("foo4()","from __main__ import foo4")
    
    >>> print "%.2f usec/pass" % (1000000 * t1.timeit(number=10000)/10000)
    100.95 usec/pass
    >>> print "%.2f usec/pass" % (1000000 * t2.timeit(number=10000)/10000)
    158.90 usec/pass
    >>> print "%.2f usec/pass" % (1000000 * t3.timeit(number=10000)/10000)
    130.02 usec/pass
    >>> print "%.2f usec/pass" % (1000000 * t4.timeit(number=10000)/10000)
    133.68 usec/pass
    >>> 
    

    Conclusion:

    Generator expressions are powerful and you can optimize it to a much greater extend. As you can see in the example foo2, which is the slowest it had the hard time appending a single list which killed the performance. foo3 and foo4 has almost the same time so it seems creating a temporary list was not a bottleneck, as it was only created once in the whole iteration. Without generators you would soon end up with some performance issues like appending a list or creating temporary lists. So lazy evaluation came to the picture to give an edge over these performance bottlenecks.

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

Sidebar

Related Questions

okay, this is probably really simple, but i looked all over the site about
okay so this is probably a soft pitch question for sombody, but I want
Okay, so this probably sounds terribly nefarious, but I need such capabilities for my
Okay, I know that 1) this is probably not possible with CSS alone and
Okay so im working on this php image upload system but for some reason
Okay, this is (probably) a very simple question, but I am afraid I know
This is probably a really easy question to answer, but for some reason I'm
Okay. This is propably very basic and noobie, but I have about 20+ stores
Okay, I know this is probably dead simple, but I can't seem to find
Okay, now I've got this thing I need to do with Javascript but I

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.