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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T18:50:44+00:00 2026-05-24T18:50:44+00:00

Requirements: I need to grow an array arbitrarily large from data. I can guess

  • 0

Requirements:

  • I need to grow an array arbitrarily large from data.
  • I can guess the size (roughly 100-200) with no guarantees that the array will fit every time
  • Once it is grown to its final size, I need to perform numeric computations on it, so I’d prefer to eventually get to a 2-D numpy array.
  • Speed is critical. As an example, for one of 300 files, the update() method is called 45 million times (takes 150s or so) and the finalize() method is called 500k times (takes total of 106s) … taking a total of 250s or so.

Here is my code:

def __init__(self):
    self.data = []

def update(self, row):
    self.data.append(row)

def finalize(self):
    dx = np.array(self.data)

Other things I tried include the following code … but this is waaaaay slower.

def class A:
    def __init__(self):
        self.data = np.array([])

    def update(self, row):
        np.append(self.data, row)

    def finalize(self):
        dx = np.reshape(self.data, size=(self.data.shape[0]/5, 5))

Here is a schematic of how this is called:

for i in range(500000):
    ax = A()
    for j in range(200):
         ax.update([1,2,3,4,5])
    ax.finalize()
    # some processing on ax
  • 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-24T18:50:44+00:00Added an answer on May 24, 2026 at 6:50 pm

    I tried a few different things, with timing.

    import numpy as np
    
    1. The method you mention as slow: (32.094 seconds)

      class A:
      
          def __init__(self):
              self.data = np.array([])
      
          def update(self, row):
              self.data = np.append(self.data, row)
      
          def finalize(self):
              return np.reshape(self.data, newshape=(self.data.shape[0]/5, 5))
      
    2. Regular ol Python list: (0.308 seconds)

      class B:
      
          def __init__(self):
              self.data = []
      
          def update(self, row):
              for r in row:
                  self.data.append(r)
      
          def finalize(self):
              return np.reshape(self.data, newshape=(len(self.data)/5, 5))
      
    3. Trying to implement an arraylist in numpy: (0.362 seconds)

      class C:
      
          def __init__(self):
              self.data = np.zeros((100,))
              self.capacity = 100
              self.size = 0
      
          def update(self, row):
              for r in row:
                  self.add(r)
      
          def add(self, x):
              if self.size == self.capacity:
                  self.capacity *= 4
                  newdata = np.zeros((self.capacity,))
                  newdata[:self.size] = self.data
                  self.data = newdata
      
              self.data[self.size] = x
              self.size += 1
      
          def finalize(self):
              data = self.data[:self.size]
              return np.reshape(data, newshape=(len(data)/5, 5))
      

    And this is how I timed it:

    x = C()
    for i in xrange(100000):
        x.update([i])
    

    So it looks like regular old Python lists are pretty good 😉

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

Sidebar

Related Questions

I need to batch create images with text. Requirements: arbitrary size of bitmap PNG
First the requirements: By management requirements, I can't use open source code. I need
I need a database that can store a large number of BLOBS. The BLOBs
I am working with google map. According to requirements i need to set different
I need assistance finding a delivery method that best fulfills the following requirements: We
I need to validate a phone number in javascript. The requirements are: they should
Due to specific requirements [*], I need a singly-linked list implementation that uses integer
My requirements: Support .NET Compact Framework 2.0 and Windows Mobile 6.0 devices. Only need
I need to build a simple, single user database application for Windows. Main requirements
I have a need for a counter of type long with the following requirements/facts:

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.