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

  • Home
  • SEARCH
  • 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 111841
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T02:27:02+00:00 2026-05-11T02:27:02+00:00

Well I was reading this post and then I came across a code which

  • 0

Well I was reading this post and then I came across a code which was:

jokes=range(1000000) domain=[(0,(len(jokes)*2)-i-1) for i in range(0,len(jokes)*2)] 

I thought wouldn’t it be better to calculate the value of len(jokes) once outside the list comprehension?

Well I tried it and timed three codes

jv@Pioneer:~$ python -m timeit -s 'jokes=range(1000000);domain=[(0,(len(jokes)*2)-i-1) for i in range(0,len(jokes)*2)]' 10000000 loops, best of 3: 0.0352 usec per loop jv@Pioneer:~$ python -m timeit -s 'jokes=range(1000000);l=len(jokes);domain=[(0,(l*2)-i-1) for i in range(0,l*2)]' 10000000 loops, best of 3: 0.0343 usec per loop jv@Pioneer:~$ python -m timeit -s 'jokes=range(1000000);l=len(jokes)*2;domain=[(0,l-i-1) for i in range(0,l)]' 10000000 loops, best of 3: 0.0333 usec per loop 

Observing the marginal difference 2.55% between the first and the second made me think – is the first list comprehension

domain=[(0,(len(jokes)*2)-i-1) for i in range(0,len(jokes)*2)] 

optimized internally by python? or is 2.55% a big enough optimization (given that the len(jokes)=1000000)?

If this is – What are the other implicit/internal optimizations in Python ?

What are the developer's rules of thumb for optimization in Python?

Edit1: Since most of the answers are ‘don’t optimize, do it later if its slow’ and I got some tips and links from Triptych and Ali A for the do’s. I will change the question a bit and request for don’ts.

Can we have some experiences from people who faced the ‘slowness‘, what was the problem and how it was corrected?

Edit2: For those who haven’t here is an interesting read

Edit3: Incorrect usage of timeit in question please see dF’s answer for correct usage and hence timings for the three codes.

  • 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. 2026-05-11T02:27:02+00:00Added an answer on May 11, 2026 at 2:27 am

    You’re not using timeit correctly: the argument to -s (setup) is a statement to be executed once initially, so you’re really just testing an empty statement. You want to do

    $ python -m timeit -s 'jokes=range(1000000)' 'domain=[(0,(len(jokes)*2)-i-1) for i in range(0, len(jokes)*2)]' 10 loops, best of 3: 1.08 sec per loop $ python -m timeit -s 'jokes=range(1000000)' 'l=len(jokes);domain=[(0,(l*2)-i-1) for i in range(0, l*2)]' 10 loops, best of 3: 908 msec per loop $ python -m timeit -s 'jokes=range(1000000)' 'l=len(jokes*2);domain=[(0,l-i-1) for i in range(0, l)]' 10 loops, best of 3: 813 msec per loop 

    While the speedup is still not dramatic, it’s more significant (16% and 25% respectively). So since it doesn’t make the code any more complicated, this simple optimization is probably worth it.

    To address the actual question… the usual rule of thumb in Python is to

    1. Favor straightforward and readable code over optimization when coding.

    2. Profile your code (profile / cProfile and pstats are your friends) to figure out what you need to optimize (usually things like tight loops).

    3. As a last resort, re-implement these as C extensions, which is made much easier with tools like pyrex and cython.

    One thing to watch out for: compared to many other languages, function calls are relatively expensive in Python which is why the optimization in your example made a difference even though len is O(1) for lists.

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

Sidebar

Ask A Question

Stats

  • Questions 134k
  • Answers 134k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer The only way I've figured out how to do this… May 12, 2026 at 6:44 am
  • Editorial Team
    Editorial Team added an answer Predefined macros depend on the standard and the way the… May 12, 2026 at 6:44 am
  • Editorial Team
    Editorial Team added an answer You only need to install the Office Interop Assemblies and… May 12, 2026 at 6:44 am

Related Questions

Background I'm writing an asynchronous comment system for my website, after reading plenty of
My friend was given this free google website optimizer tshirt and came to me
I wanted to get a feedback from you guys (and hopefully some references to
I was reading about this person's interview at a well-known search company. http://asserttrue.blogspot.com/2009/05/one-of-toughest-job-interview-questions.html He

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.