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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T14:41:34+00:00 2026-05-14T14:41:34+00:00

I have a function foo(i) that takes an integer and takes a significant amount

  • 0

I have a function foo(i) that takes an integer and takes a significant amount of time to execute. Will there be a significant performance difference between any of the following ways of initializing a:

a = [foo(i) for i in xrange(100)]

a = map(foo, range(100))

vfoo = numpy.vectorize(foo)
a = vfoo(range(100))

(I don’t care whether the output is a list or a numpy array.)

Is there a better way?

  • 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-05-14T14:41:35+00:00Added an answer on May 14, 2026 at 2:41 pm

    First comment: don’t mix usage of xrange() or range() in your samples… doing so invalidates your question as you’re comparing apples and oranges.

    I second @Gabe’s notion that if you have many large data structures, numpy should win overall… just keep in mind most of the time C is faster than Python, but then again, most of the time, PyPy is faster than CPython. :-)

    As far as listcomps vs. map() calls go… one makes 101 function calls while the other one makes 102. meaning you won’t see a significant difference in timing, as shown below using the timeit module as @Mike has suggested:

    • List Comprehension

      $ python -m timeit "def foo(x):pass; [foo(i) for i in range(100)]"
      1000000 loops, best of 3: 0.216 usec per loop
      $ python -m timeit "def foo(x):pass; [foo(i) for i in range(100)]"
      1000000 loops, best of 3: 0.21 usec per loop
      $ python -m timeit "def foo(x):pass; [foo(i) for i in range(100)]"
      1000000 loops, best of 3: 0.212 usec per loop

    • map() function call

      $ python -m timeit "def foo(x):pass; map(foo, range(100))"
      1000000 loops, best of 3: 0.216 usec per loop
      $ python -m timeit "def foo(x):pass; map(foo, range(100))"
      1000000 loops, best of 3: 0.214 usec per loop
      $ python -m timeit "def foo(x):pass; map(foo, range(100))"
      1000000 loops, best of 3: 0.215 usec per loop

    With that said however, unless you are planning on using the lists that you create from either of these techniques, try avoid them (using lists) completely. IOW, if all you’re doing is iterating over them, it’s not worth the memory consumption (and possibly creating a potentially massive list in memory) when you only care to look at each element one at a time just discard the list as soon as you’re done.

    In such cases, I highly recommend the use of generator expressions instead as they don’t create the entire list in memory… it is a more memory-friendly, lazy iterative way of looping through elements to process w/o creating a largish array in memory. The best part is that its syntax is nearly identical to that of listcomps:

    a = (foo(i) for i in range(100))
    

    2.x users only: along the lines of more iteration, change all the range() calls to xrange() for any older 2.x code then switch to range() when porting to Python 3 where xrange() replaces and is renamed to range().

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

Sidebar

Ask A Question

Stats

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

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

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

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

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer You can't literally merge several compiled .dll files into one.… May 15, 2026 at 7:11 pm
  • Editorial Team
    Editorial Team added an answer You need to get the handle of the installer window.… May 15, 2026 at 7:11 pm
  • Editorial Team
    Editorial Team added an answer Update: Here's a working example: public class MainView extends JFrame… May 15, 2026 at 7:11 pm

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.