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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T14:06:03+00:00 2026-06-06T14:06:03+00:00

In doing some bioinformatics work, I’ve been pondering the ramifications of storing object instances

  • 0

In doing some bioinformatics work, I’ve been pondering the ramifications of storing object instances in a Numpy array rather than a Python list, but in all the testing I’ve done the performance was worse in every instance. I am using CPython. Does anyone know the reason why?

Specifically:

  • What are the performance impacts of using a fixed-length array numpy.ndarray(dtype=object) vs. a regular Python list? Initial tests I performed showed that accessing the Numpy array elements was slower than iteration through the Python list, especially when using object methods.
  • Why is it faster to instantiate objects using a list comprehension such as [ X() for i in range(n) ] instead of a numpy.empty(size=n, dtype=object)?
  • What is the memory overhead of each? I was not able to test this. My classes extensively use __slots__, if that has any impact.
  • 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-06T14:06:04+00:00Added an answer on June 6, 2026 at 2:06 pm

    Don’t use object arrays in numpy for things like this.

    They defeat the basic purpose of a numpy array, and while they’re useful in a tiny handful of situations, they’re almost always a poor choice.

    Yes, accessing an individual element of a numpy array in python or iterating through a numpy array in python is slower than the equivalent operation with a list. (Which is why you should never do something like y = [item * 2 for item in x] when x is a numpy array.)

    Numpy object arrays will have a slightly lower memory overhead than a list, but if you’re storing that many individual python objects, you’re going to run into other memory problems first.

    Numpy is first and foremost a memory-efficient, multidimensional array container for uniform numerical data. If you want to hold arbitrary objects in a numpy array, you probably want a list, instead.


    My point is that if you want to use numpy effectively, you may need to re-think how you’re structuring things.

    Instead of storing each object instance in a numpy array, store your numerical data in a numpy array, and if you need separate objects for each row/column/whatever, store an index into that array in each instance.

    This way you can operate on the numerical arrays quickly (i.e. using numpy instead of list comprehensions).

    As a quick example of what I’m talking about, here’s a trivial example without using numpy:

    from random import random
    
    class PointSet(object):
        def __init__(self, numpoints):
            self.points = [Point(random(), random()) for _ in xrange(numpoints)]
    
        def update(self):
            for point in self.points:
                point.x += random() - 0.5
                point.y += random() - 0.5
    
    class Point(object):
        def __init__(self, x, y):
            self.x = x
            self.y = y
    
    points = PointSet(100000)
    point = points.points[10]
    
    for _ in xrange(1000):
        points.update()
        print 'Position of one point out of 100000:', point.x, point.y
    

    And a similar example using numpy arrays:

    import numpy as np
    
    class PointSet(object):
        def __init__(self, numpoints):
            self.coords = np.random.random((numpoints, 2))
            self.points = [Point(i, self.coords) for i in xrange(numpoints)]
    
        def update(self):
            """Update along a random walk."""
            # The "+=" is crucial here... We have to update "coords" in-place, in
            # this case. 
            self.coords += np.random.random(self.coords.shape) - 0.5
    
    class Point(object):
        def __init__(self, i, coords):
            self.i = i
            self.coords = coords
    
        @property
        def x(self):
            return self.coords[self.i,0]
    
        @property
        def y(self):
            return self.coords[self.i,1]
    
    
    points = PointSet(100000)
    point = points.points[10]
    
    for _ in xrange(1000):
        points.update()
        print 'Position of one point out of 100000:', point.x, point.y
    

    There are other ways to do this (you may want to avoid storing a reference to a specific numpy array in each point, for example), but I hope it’s a useful example.

    Note the difference in speed at which they run. On my machine, it’s a difference of 5 seconds for the numpy version vs 60 seconds for the pure-python version.

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

Sidebar

Related Questions

Doing some project clean up, and rather than just relying on my human eyes
Doing some homework here (second assignment, still extremely green...). The object is to read
After doing some research on the subject, I've been experimenting a lot with patterns
Just doing some work with RIA services and I see in the MSDN documentation
Recently I'm doing some work on RTMP streaming, that is using Flowplayer to integrate
doing some quick web dev work for a local body modification parlour just in
After doing some processing on an audio or image array, it needs to be
Been doing some Android permission research and ran across an application that - according
Doing some work on a Drawing Canvas and I'm wanting to implement a Redo
Doing some integration work with another site I've got the unusual requirement of needing

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.