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 5 2187
Tobiah <to**@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
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
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.
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
On Mar 17, 1:49 am, "Diez B. Roggisch" <de***@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.) This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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 = PyArray_FromDimsAndData(1, int*dim, PyArray_DOUBLE,
char*buf);
Users will get a Numeric Array object and can change its values (and
actually change the...
|
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, GetEnumerator, and
RemoveAt) but the way it implements all the other things you need to
override seems overkill and counters the advantage of having an...
|
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 module is "imageio" and the reader function from the file
is image_read() which returns an object ( "filled" C structure) with a
lot of members.
|
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 is, how to make python read from a C array? By reading the
documentation, one could get the impression that PyBufferObjects do that
job.
|
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'
55:{'name': 'Hello1',
'fields':'fields1',
'valid': 'valid1'},
| |
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
|
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 I'm
going to start next month.
The tilemap editor is working fine, but reading Guido's Van Rossum
PYTHON TUTORIAL I found that my game map is...
|
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.
|
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.
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language...
|
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it. ...
| |
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert...
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |