It's not a matter of *what* can be done, but how *efficiently* it can be
done. Under the best of circumstances a list (or dynamically sized and
mutable sequence) can't perform at the level of a statically sized array.
Even if you take into account the overhead necessary to resize the array
(should that be necessary), such an approach will outperform a list. If
you don't have to do insertions and deletions, an array (i.e., a genuine
array) will make a substantial difference in runtime performance.
Likewise, there is (or should be) less memory overhead with an array, but
that's a minor and largely theoretical issue from my point of view.
For the most part, this won't matter to many programmers -- who don't
encounter circumstances where serious performance matters. And so they
gain nothing from using an array rather than a sequence. And in general
(in a language like Python) sequences are so much easier and more natural
to use. But in circumstances where you have a very large number of items
that you know you can index directly via integers and where you don't need
to do inserts or deletes to any significant degree, a more efficient data
structure can offer a real gain.
At the moment in my Python apps I use the usual sequences, converting
things to tuples in cases where I know they won't later need to be
changed. But I'm still prototyping. When we go production, a hard look
will be taken at performance (or, if circumstances dictate, even before
that). And at that point it may be necessary to implement some extensions
in C. Greg Ewing, for example, recently told me that he has rewritten Plex
by using Pyrex and achieved a truly substantial performance improvement
(not yet released). For the amount of data I'm dealing with, and the
requirements for interactive use, these things are likely to make a
difference.
To be honest, I've been quite pleasantly surprised about the performance of
Python in general and such things as wxPython and the Python bindings for
BSDDB -- though of course in the latter two cases most of that performance
comes from the underlying C/C++ implentations :-).
···
--------------------------------------
Gary H. Merrill
Director and Principal Scientist, New Applications
Data Exploration Sciences
GlaxoSmithKline Inc.
(919) 483-8456
Well it is still unclear exactly what you want to do but it also sounds like
you are suffering from premature optimization disease and assumptions of
performance based on other languages you've used. I'm not trying to flame
you, that is fairly common (we've all been there) and the cure is to just
try a couple of variations, which is easy enough to do in Python. If
performance isn't adequate you should know pretty quick, but I seriously
doubt an array is what you want.
In your case, I think what you're looking for is a dictionary, not a list.
http://www.python.org/doc/current/lib/typesmapping.html
If you want to use integers as the keys you can, but you can just as easily
use another immutable type like a string. Maybe something like:
buttons = {}
buttons[1] = wx.wxButton(panel, -1, 'Button 1', (0, 30))
buttons[1].GetLabel()
'Button 1'
If you need an "ordered" dictionary, see the Python Cookbook or ask on
comp.lang.python for numerous recipes.
ka
···
-----Original Message-----
From: gary.h.merrill@gsk.com [mailto:gary.h.merrill@gsk.com]
Sent: Wednesday, August 06, 2003 7:28 AM
To: wxPython-users@lists.wxwindows.org
Subject: Re: [wxPython-users] Creating Buttons referenced like an array
It's not a matter of *what* can be done, but how *efficiently* it can be
done. Under the best of circumstances a list (or dynamically sized and
mutable sequence) can't perform at the level of a statically sized array.
Even if you take into account the overhead necessary to resize the array
(should that be necessary), such an approach will outperform a list. If
you don't have to do insertions and deletions, an array (i.e., a genuine
array) will make a substantial difference in runtime performance.
Likewise, there is (or should be) less memory overhead with an array, but
that's a minor and largely theoretical issue from my point of view.
For the most part, this won't matter to many programmers -- who don't
encounter circumstances where serious performance matters. And so they
gain nothing from using an array rather than a sequence. And in general
(in a language like Python) sequences are so much easier and more natural
to use. But in circumstances where you have a very large number of items
that you know you can index directly via integers and where you don't need
to do inserts or deletes to any significant degree, a more efficient data
structure can offer a real gain.
At the moment in my Python apps I use the usual sequences, converting
things to tuples in cases where I know they won't later need to be
changed. But I'm still prototyping. When we go production, a hard look
will be taken at performance (or, if circumstances dictate, even before
that). And at that point it may be necessary to implement some extensions
in C. Greg Ewing, for example, recently told me that he has
rewritten Plex
by using Pyrex and achieved a truly substantial performance improvement
(not yet released). For the amount of data I'm dealing with, and the
requirements for interactive use, these things are likely to make a
difference.
To be honest, I've been quite pleasantly surprised about the
performance of
Python in general and such things as wxPython and the Python bindings for
BSDDB -- though of course in the latter two cases most of that performance
comes from the underlying C/C++ implentations :-).
--------------------------------------
Gary H. Merrill
Director and Principal Scientist, New Applications
Data Exploration Sciences
GlaxoSmithKline Inc.
(919) 483-8456