473,386 Members | 1,819 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

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:pytesttime 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

Mar 14 '08 #1
5 2180
Tobiah <to**@tobiah.orgwrites:
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:pytesttime 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.4988760948181152
>>from array import array
arr = lambda n: array('i', n)
timeit.Timer('test(arr, n)', 'from __main__ import test, arr, n').timeit(1)
5.7419960498809814
>>>
--
Arnaud
Mar 16 '08 #2
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
Mar 16 '08 #3
On 13 Mar, 20:40, Tobiah <t...@tobiah.orgwrote:
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.




Mar 16 '08 #4
sturlamolden schrieb:
On 13 Mar, 20:40, Tobiah <t...@tobiah.orgwrote:
>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
Mar 16 '08 #5
On Mar 17, 1:49 am, "Diez B. Roggisch" <de***@nospam.web.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:list_resize and arraymodule.c:array_resize.)
Mar 28 '08 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Bo Peng | last post by:
Dear list, I am writing a Python extension module that needs a way to expose pieces of a big C array to python. Currently, I am using NumPy like the following: PyObject* res =...
2
by: Samuel R. Neff | last post by:
What's the advantage of inheriting from CollectionBase as opposed to just implementing IList? It seems that it saves you from having to implement a few properties (Clear, CopyTo, Count,...
5
by: Krish | last post by:
Hello People I hope I am On Topic. Anyways, here is my problem. Any insights would be really appreciated. I have wrapped a C IO module using SWIG -> Python Module. Suppose the name of the...
6
by: Peter Wuertz | last post by:
Hi, I'm writing a C module for python, that accesses a special usb camera. This module is supposed to provide python with data (alot of data). Then SciPy is used to fit the data. My question...
3
by: Steven W. Orr | last post by:
This is more for my education and not so much for practicality. I have a structure that sort of looks like this: mdict = {33:{'name': 'Hello0', 'fields':'fields0', 'valid': 'valid0'...
6
by: Gilles Ganault | last post by:
Hello I'm sure there's a much easier way to read a two-column, CSV file into an array, but I haven't found it in Google. Should I use the Array module instead? ========= a = i = 0
8
by: Santiago Romero | last post by:
Hi :) First of all, I must apologize for my poor english :) I'm starting with python and pygame and for testing (and learning) purposes I wrote an small "Map Editor" for a small game project...
10
by: J. Peng | last post by:
what's the difference between an array and a list in python? I see list has all features of array in C or perl. so please tell me.thanks.
6
by: Jillian Calderon | last post by:
Hi. How do I define a 2d list? For instance, to define a 4 by 5 list, I wanted to do this: n=4 m=5 world = However, it gives me an invalid syntax error saying the index is out of range.
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.