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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T08:17:52+00:00 2026-05-31T08:17:52+00:00

I ran a simple speed test comparing numpy and python list comprehension, and apparently

  • 0

I ran a simple speed test comparing numpy and python list comprehension, and apparently list comprehension was faster. Is that correct?

import sys, numpy
from datetime import datetime

def numpysum(n):
    a = numpy.arange(n) ** 2
    b = numpy.arange(n) ** 3
    return a + b

def pythonsum(n):
    a = [i ** 2 for i in range(n)]
    b = [i ** 3 for i in range(n)]
    return [a[i] + b[i] for i in range(n)]

size = 10
start = datetime.now()
c1 = pythonsum(size)
delta1 = datetime.now() - start

start = datetime.now()
c2 = numpysum(size)
delta2 = datetime.now() - start

print c1
print c2

print delta1
print delta2
  • 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-31T08:17:53+00:00Added an answer on May 31, 2026 at 8:17 am

    I think you might want to consider varying your testing parameter:

    In [39]: %timeit pythonsum(10)
    100000 loops, best of 3: 8.41 us per loop
    
    In [40]: %timeit pythonsum(100)
    10000 loops, best of 3: 51.9 us per loop
    
    In [41]: %timeit pythonsum(1000)
    1000 loops, best of 3: 451 us per loop
    
    In [42]: %timeit pythonsum(10000)
    100 loops, best of 3: 17.9 ms per loop
    
    In [43]: %timeit numpysum(10)
    100000 loops, best of 3: 13.4 us per loop
    
    In [44]: %timeit numpysum(100)
    100000 loops, best of 3: 17 us per loop
    
    In [45]: %timeit numpysum(1000)
    10000 loops, best of 3: 50.3 us per loop
    
    In [46]: %timeit numpysum(10000)
    1000 loops, best of 3: 385 us per loop
    

    Ratio of Numpy vs List comprehension timings:

    10: 0.6x

    100: 3.1x

    1000: 9x

    10000: 46x

    Thus, Numpy is much faster for large N.

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

Sidebar

Related Questions

We made a simple application and using GoogleAppEngineLauncher (GAEL) ran that locally. Then we
After having some trouble with object deletion, I ran a simple test: Created a
I ran the following simple query that looks like the example below: (Briefly; one
I just ran a simple MySQL CREATE TABLE statement that produced the line Query
I ran across a compilation issue today that baffled me. Consider these two container
I just wrote my first OpenMP program that parallelizes a simple for loop. I
I just ran this simple code through the w3c validation service ( http://validator.w3.org/check ).
An assembly program is converted to binary code and I ran a simple code
This is a simple query ran when the user presses logout from my website
I'm developing a simple database architecture in VisualParadigm and lately ran over next code

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.