Memory pre-allocation for large lists

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Avi Kak

    Memory pre-allocation for large lists



    Hello:

    Is it possible in Python to define an empty list
    of a predetermined size?

    To illustrate my question further, I can do the
    following in Perl

    my @arr = ();
    $#arr = 999;

    that wold cause @arr to become an empty array
    of size 1000. This array could be used to
    store 1000 elements without any further memory
    allocation for the array itself.

    Can the same be done in Python?

    Avi Kak
    kak@purdue.edu


  • Erik Max Francis

    #2
    Re: Memory pre-allocation for large lists

    Avi Kak wrote:
    [color=blue]
    > Can the same be done in Python?[/color]

    Not directly, but you can just fill it with something you're not going
    to use:

    arra = [None] * 999

    --
    __ Erik Max Francis && max@alcyone.com && http://www.alcyone.com/max/
    / \ San Jose, CA, USA && 37 20 N 121 53 W && &tSftDotIotE
    \__/ Of war men ask the outcome, not the cause.
    -- Seneca

    Comment

    • Avi Kak

      #3
      Re: Memory pre-allocation for large lists



      Thanks, Erik. That would work for me.

      Avi


      On Tue, 17 Feb 2004 22:00:09 -0800, Erik Max Francis <max@alcyone.co m>
      wrote:
      [color=blue]
      >Avi Kak wrote:
      >[color=green]
      >> Can the same be done in Python?[/color]
      >
      >Not directly, but you can just fill it with something you're not going
      >to use:
      >
      > arra = [None] * 999[/color]

      Comment

      • Aahz

        #4
        Re: Memory pre-allocation for large lists

        In article <ift530d51l8td0 uoohm5t9ad96pq5 n4e3i@4ax.com>,
        Avi Kak <kak@purdue.edu > wrote:[color=blue]
        >
        >Is it possible in Python to define an empty list of a predetermined
        >size?
        >
        >To illustrate my question further, I can do the following in Perl
        >
        > my @arr = ();
        > $#arr = 999;
        >
        >that wold cause @arr to become an empty array of size 1000. This
        >array could be used to store 1000 elements without any further memory
        >allocation for the array itself.
        >
        >Can the same be done in Python?[/color]

        Can, yes. But why? Python uses an allocation mechanism for lists that
        results in constant amortized time; there's really no reason to
        pre-allocate.
        --
        Aahz (aahz@pythoncra ft.com) <*> http://www.pythoncraft.com/

        "Argue for your limitations, and sure enough they're yours." --Richard Bach

        Comment

        Working...