Python is faster than C
Christian Tismer
pirx at python.net
Sat Apr 3 20:59:16 EST 2004
Armin Rigo <arigo <at> tunes.org> writes:
> Again I'm not saying it is trivial to implement it, but that not having
> to expose for optimization purposes 'xrange' and the whole 'iterator'
> part of the language would be worth it, in my opinion.
First of all, I'm trying to see whether I can write through this interface.
As you might have realized, sarcastically after they fooled me with that
April joke, my site was really lost, andthis is a tad.
Anyway, I'd like to add that Armin's idea can be extended (as he surely knows)
to special casing seldom assignments to and algorithmically given array.
That is, in the case of just a few assignments, a list could internally
aufment the expression. If the number of elements grows, it could be
turned into a preferred dictionary, after reaching some threshold.
And after another threshold, it could be turned into something like
a real list, or just a specialized, typed list, depending on the type.
In general, I share Armin's impression, that iterators are nothing else
but an explicit way to spell optimizations.
While explicit is better than implicit, in the case of optimizations,
I believe it is an over-specification, and almost completely in the false
direction. We have to prove this in a known project, still.
There is one thing where I think explicit iterator do make some sense,
in a way the reader might not expect.
Let me show:
if you do something like
for each in sequence:
try:
do_something_with(each)
except SomeThingWrongWithThis:
# handle exception somehow
In terms of iterators, we implicitly create an interator here and consume it.
The explicit notation of iterators gives us this advantage:
instead you can do it this way:
it = iter(sequence)
can_continue = 1
while can_continue:
try:
for each in it:
do_something_with(each)
exceptSomeThingWrongWithThis
can_continue = some_recovery(each)
continue
In words: By the help of iterators, it is possible to write exception
handlers for special cases *outside* the loop, repair the error, and
continue iterating in the loop.
I have used this pattern a lot of times now, and I'm quite happy ith it.
But I have to admit, that this is too a bad, explicit optimization, and
a compiler could be smart enough to do this for me, automatically.
cheers - chris
More information about the Python-list
mailing list