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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T05:55:50+00:00 2026-05-28T05:55:50+00:00

I’m trying to speed up part of my code that involves looping through and

  • 0

I’m trying to speed up part of my code that involves looping through and setting the values in a large 2D array. One of the suggestions was that I try pre-allocating the array rather than using .append() but it was pointed out that in Python .append() is an amortized O(1) operation.

However when I tested it using the following code:

import time

x = list()
z = list()
t1 = time.time()
for i in range(10000):
    z.append([])
    for j in range(10000):
        z[i].append(0)

t1 = time.time()
for i in range(10000):
    x.append([])
    for j in range(10000):
        x[i].append(1)
print(time.time()-t1)

t1 = time.time()
for i in range(10000):
    for j in range(10000):
        z[i][j] = 1
print(time.time()-t1)

I consitently get the pre-allocated array taking 3-4 seconds less than the array that isn’t preallocated (~17s compared to ~21). What is it in this code that is causing the .append() based function to take longer than replacing the value in a pre-allocated array?

  • 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-28T05:55:50+00:00Added an answer on May 28, 2026 at 5:55 am

    Consider the following:

    from dis import dis
    
    def f1():
     x = []
     for i in range(10000):
      x.append([])
      for j in range(10000):
       x[i].append(0)
     return x
    
    dis(f1)
    
      2           0 BUILD_LIST               0
                  3 STORE_FAST               0 (x)
    
      3           6 SETUP_LOOP              73 (to 82)
                  9 LOAD_GLOBAL              0 (range)
                 12 LOAD_CONST               1 (10000)
                 15 CALL_FUNCTION            1
                 18 GET_ITER
            >>   19 FOR_ITER                59 (to 81)
                 22 STORE_FAST               1 (i)
    
      4          25 LOAD_FAST                0 (x)
                 28 LOAD_ATTR                1 (append)
                 31 BUILD_LIST               0
                 34 CALL_FUNCTION            1
                 37 POP_TOP
    
      5          38 SETUP_LOOP              37 (to 78)
                 41 LOAD_GLOBAL              0 (range)
                 44 LOAD_CONST               1 (10000)
                 47 CALL_FUNCTION            1
                 50 GET_ITER
            >>   51 FOR_ITER                23 (to 77)
                 54 STORE_FAST               2 (j)
    
      6          57 LOAD_FAST                0 (x)
                 60 LOAD_FAST                1 (i)
                 63 BINARY_SUBSCR
                 64 LOAD_ATTR                1 (append)
                 67 LOAD_CONST               2 (0)
                 70 CALL_FUNCTION            1
                 73 POP_TOP
                 74 JUMP_ABSOLUTE           51
            >>   77 POP_BLOCK
            >>   78 JUMP_ABSOLUTE           19
            >>   81 POP_BLOCK
    
      7     >>   82 LOAD_FAST                0 (x)
                 85 RETURN_VALUE
    

    Compared with:

    def f2():
     x = list()
     for i in range(10000):
      x.append([0]*10000)
     return x
    
    dis(f2)
    
      2           0 LOAD_GLOBAL              0 (list)
                  3 CALL_FUNCTION            0
                  6 STORE_FAST               0 (x)
    
      3           9 SETUP_LOOP              40 (to 52)
                 12 LOAD_GLOBAL              1 (range)
                 15 LOAD_CONST               1 (10000)
                 18 CALL_FUNCTION            1
                 21 GET_ITER
            >>   22 FOR_ITER                26 (to 51)
                 25 STORE_FAST               1 (i)
    
      4          28 LOAD_FAST                0 (x)
                 31 LOAD_ATTR                2 (append)
                 34 LOAD_CONST               2 (0)
                 37 BUILD_LIST               1
                 40 LOAD_CONST               1 (10000)
                 43 BINARY_MULTIPLY
                 44 CALL_FUNCTION            1
                 47 POP_TOP
                 48 JUMP_ABSOLUTE           22
            >>   51 POP_BLOCK
    
      5     >>   52 LOAD_FAST                0 (x)
                 55 RETURN_VALUE
    

    How you approach things can make a huge difference.

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

Sidebar

Related Questions

I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I'm trying to create an if statement in PHP that prevents a single post
I am trying to loop through a bunch of documents I have to put
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I've got a string that has curly quotes in it. I'd like to replace
I have a French site that I want to parse, but am running into
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString

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.