Strange array.array performance

Discussion in 'Python' started by Maxim Khitrov, Feb 19, 2009.

  1. Hello all,

    I'm currently writing a Python <-> MATLAB interface with ctypes and
    array.array class, using which I'll need to push large amounts of data
    to MATLAB. Everything is working well, but there was one strange
    performance-related issue that I ran into and wanted to ask about.
    Here's some example code to illustrate my point (this is developed on
    Windows, hence the use of clock):

    ---
    from array import array
    from time import clock

    input = array('B', range(256) * 10000)

    # Case 1
    start = clock()
    data1 = array('B', input)
    print format(clock() - start, '.10f')

    # Case 2
    start = clock()
    data2 = array('B')
    data2[:] = input
    print format(clock() - start, '.10f')

    # Case 3
    start = clock()
    data3 = array('B')
    data3.extend(input)
    print format(clock() - start, '.10f')

    print input == data1 == data2 == data3
    ---

    The output from this on my machine is as follows:

    0.7080547730
    0.0029827034
    0.0028685943
    True

    That seems very wrong. In the end, all arrays have the same data, but
    by specifying it in the constructor the creation process takes over
    350x longer than the other two methods. Is this a bug, or is there
    some valid reason for it?

    In the latter case, it would be a good idea to mention this in the
    documentation, since that can be a significant performance improvement
    in some applications. Currently the documentation states "Otherwise,
    the iterable initializer is passed to the extend() method," which
    doesn't seem to be the case, based on the third example.

    - Max
    Maxim Khitrov, Feb 19, 2009
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. jm
    Replies:
    1
    Views:
    500
    alien2_51
    Dec 12, 2003
  2. z. f.
    Replies:
    0
    Views:
    670
    z. f.
    Feb 3, 2005
  3. Sojwal
    Replies:
    2
    Views:
    312
    Chris Uppal
    Jan 27, 2004
  4. Maxim Khitrov

    Re: Strange array.array performance

    Maxim Khitrov, Feb 19, 2009, in forum: Python
    Replies:
    5
    Views:
    368
  5. Software Engineer
    Replies:
    0
    Views:
    313
    Software Engineer
    Jun 10, 2011
Loading...

Share This Page