Advantage of the array module over lists?

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

    Advantage of the array module over lists?

    I checked out the array module today. It claims that
    arrays are 'efficient'. I figured that this must mean
    that they are faster than lists, but this doesn't seem
    to be the case:

    ############### # one.py ##############
    import array

    a = array.array('i' )

    for x in xrange(10000000 ):
    a.append(x)

    for x in a:
    a[x] += 1

    ############### # two.py ##############
    a = []

    for x in xrange(10000000 ):
    a.append(x)

    for x in a:
    a[x] += 1

    ############### ############### ########


    ktops:toby:pyte sttime python one.py; time python two.py

    real 0m28.116s
    user 0m17.504s
    sys 0m10.435s

    real 0m23.026s
    user 0m13.027s
    sys 0m9.777s


    Perhaps the only advantage is that they take less memory
    to store a large number of items? It would seem then, that
    'economical' might have been a better choice of word than
    'efficient'.

    Thanks,

    Toby


    --
    Posted via a free Usenet account from http://www.teranews.com

  • Arnaud Delobelle

    #2
    Re: Advantage of the array module over lists?

    Tobiah <toby@tobiah.or gwrites:
    I checked out the array module today. It claims that
    arrays are 'efficient'. I figured that this must mean
    that they are faster than lists, but this doesn't seem
    to be the case:
    >
    ############### # one.py ##############
    import array
    >
    a = array.array('i' )
    >
    for x in xrange(10000000 ):
    a.append(x)
    >
    for x in a:
    a[x] += 1
    >
    ############### # two.py ##############
    a = []
    >
    for x in xrange(10000000 ):
    a.append(x)
    >
    for x in a:
    a[x] += 1
    >
    ############### ############### ########
    >
    >
    ktops:toby:pyte sttime python one.py; time python two.py
    >
    real 0m28.116s
    user 0m17.504s
    sys 0m10.435s
    >
    real 0m23.026s
    user 0m13.027s
    sys 0m9.777s
    >
    >
    Perhaps the only advantage is that they take less memory
    to store a large number of items? It would seem then, that
    'economical' might have been a better choice of word than
    'efficient'.
    I get an even bigger difference with this test (same as yours, but
    using timeit and using an allegedly more efficient way of initialising
    the array)
    >>def test(arr, n):
    .... a = arr(xrange(n))
    .... for x in a:
    .... a[x] += 1
    ....
    >>n = 10000000
    >>import timeit
    >>timeit.Timer( 'test(list, n)', 'from __main__ import test, n').timeit(1)
    2.4988760948181 152
    >>from array import array
    >>arr = lambda n: array('i', n)
    >>timeit.Timer( 'test(arr, n)', 'from __main__ import test, arr, n').timeit(1)
    5.7419960498809 814
    >>>
    --
    Arnaud

    Comment

    • bearophileHUGS@lycos.com

      #3
      Re: Advantage of the array module over lists?

      Their efficiency is mostly regarding the space. I think they aren't
      much speed-efficient because they require many conversions from-to
      Python types.
      You can gain speed efficiency too (sometimes a LOT), in some
      situations, using array with Psyco.
      Another advantage of arrays (better called "vector"s, probably, so the
      name "array" can replace the "list" name used by the built in) is that
      they offer you a fixed size representation, so you know what you are
      working with.
      You can also take a look at the C version of the BList from
      cheeseshop, the autor has made them rather efficient for some kinds of
      operations.

      Bye,
      bearophile

      Comment

      • sturlamolden

        #4
        Re: Advantage of the array module over lists?

        On 13 Mar, 20:40, Tobiah <t...@tobiah.or gwrote:
        I checked out the array module today. It claims that
        arrays are 'efficient'. I figured that this must mean
        that they are faster than lists, but this doesn't seem
        to be the case:
        >
        ############### # one.py ##############
        import array
        >
        a = array.array('i' )
        >
        for x in xrange(10000000 ):
        a.append(x)

        Lists are better optimized for appending to the end. Python lists are
        implemented as arrays of pointers, with a few empty slots at the
        end.

        Arrays are contigous memory buffers. They provide faster read-write
        access, particularly for chunks of data, but are slower at resizing.

        I never use the array module, as NumPy is superior.










        Comment

        • Diez B. Roggisch

          #5
          Re: Advantage of the array module over lists?

          sturlamolden schrieb:
          On 13 Mar, 20:40, Tobiah <t...@tobiah.or gwrote:
          >I checked out the array module today. It claims that
          >arrays are 'efficient'. I figured that this must mean
          >that they are faster than lists, but this doesn't seem
          >to be the case:
          >>
          >############## ## one.py ##############
          >import array
          >>
          >a = array.array('i' )
          >>
          >for x in xrange(10000000 ):
          > a.append(x)
          >
          >
          Lists are better optimized for appending to the end. Python lists are
          implemented as arrays of pointers, with a few empty slots at the
          end.
          >
          Arrays are contigous memory buffers. They provide faster read-write
          access, particularly for chunks of data, but are slower at resizing.
          I doubt that. AFAIK both arrays and lists are continuous memory-areas,
          that double (at least to a certain threshold or so) when reaching the
          capacity limit.

          lists are of type PyObject* of course, whereas arrays are not, instead
          they are of their respective primitive type, making them more memory
          efficient.

          Diez

          Comment

          • Piet Delport

            #6
            Re: Advantage of the array module over lists?

            On Mar 17, 1:49 am, "Diez B. Roggisch" <deets@nospam.w eb.dewrote:
            >
            I doubt that. AFAIK both arrays and lists are continuous memory-areas,
            that double (at least to a certain threshold or so) when reaching the
            capacity limit.
            For what it's worth, lists over-allocate by ~1/8, and arrays by ~1/16.
            (Details in listobject.c:li st_resize and arraymodule.c:a rray_resize.)

            Comment

            Working...