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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T01:31:51+00:00 2026-06-07T01:31:51+00:00

I have a generator that generates a finite sequence. To determine the length of

  • 0

I have a generator that generates a finite sequence. To determine
the length of this sequence I tried these two approaches:

 seq_len = sum([1 for _ in euler14_seq(sv)])  # list comp

and

 seq_len = sum(1 for _ in euler14_seq(sv))    # generator expression

sv is a constant starting value for the sequence.

I had expected that list comprehension would be slower and the
generator expression faster, but it turns out the other way around.

I assume the first one will be much more memory intensive since it
creates a complete list in memory first – part of the reason I also thought it would be slower.

My question: Is this observation generalizable? And is this due to
having two generators involved in the second statement vs the first?

I’ve looked at these What's the shortest way to count the number of items in a generator/iterator?, Length of generator output, and
Is there any built-in way to get the length of an iterable in python? and saw some other approaches to measuring the length of a sequence, but I’m specifically curious about the comparison of list comp vs generator expression.

PS: This came up when I decided to solve Euler Project #14 based on a
question asked on SO yesterday.

(By the way, what’s the general feeling regarding use of the '_' in
places where variable values are not needed).

This was done with Python 2.7.2 (32-bit) under Windows 7 64-bit

  • 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-07T01:31:54+00:00Added an answer on June 7, 2026 at 1:31 am

    On this computer, the generator expression becomes faster somewhere between 100,000 and 1,000,000

    $ python -m timeit "sum(1 for x in xrange(100000))"
    10 loops, best of 3: 34.8 msec per loop
    $ python -m timeit "sum([1 for x in xrange(100000)])"
    10 loops, best of 3: 20.8 msec per loop
    $ python -m timeit "sum(1 for x in xrange(1000000))"
    10 loops, best of 3: 315 msec per loop
    $ python -m timeit "sum([1 for x in xrange(1000000)])"
    10 loops, best of 3: 469 msec per loop
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a generator that generates a series, for example: def triangle_nums(): '''Generates a
I have code that generates this error: ** Message: pygobject_register_sinkfunc is deprecated (GstObject) Traceback
I have a application that generates a couple of different mails. These mails are
Suppose you have a random number generator that generates a random floating point number
in Java I have a random generator that generates random number from -2^63 to
I just read this interesting question about a random number generator that never generates
I have a C++ code generator in Python that generates many source files. Most
I am making a password generator that generates a random number, then I have
I have code that generates a List<string[]> variable but can't quite figure out how
I have a form generator that makes all the dbEdits the same width. The

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.