[Python-Dev] iterators (original) (raw)
Guido van Rossum guido@beopen.com
Mon, 21 Aug 2000 22:32:18 -0500
- Previous message: [Python-Dev] iterators
- Next message: [Python-Dev] iterators
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
[BDFL]
> The statement > > for in : > > should translate into this kind of pseudo-code: > > # variant 1 _> temp = .newiterator() > while 1: _> try: = temp.next() > except ExhaustedIterator: break > > > or perhaps (to avoid the relatively expensive exception handling): > > # variant 2 _> temp = .newiterator() > while 1: _> _flag, <variable = _temp.next()_ _> if not flag: break >
[MAL]
How about a third variant:
#3: _iter = .iterator() _while iter: _ = iter.next() This adds a slot call, but removes the malloc overhead introduced by returning a tuple for every iteration (which is likely to be a performance problem).
Are you sure the slot call doesn't cause some malloc overhead as well?
Ayway, the problem with this one is that it requires a dynamic iterator (one that generates values on the fly, e.g. something reading lines from a pipe) to hold on to the next value between "while __iter" and "__iter.next()".
Another possibility would be using an iterator attribute to get at the variable:
#4: _iter = .iterator() while 1: _if not iter.next(): break _ = iter.value
Uglier than any of the others.
You might want to check out the counterobject.c approach I used to speed up the current for-loop in Python 1.5's ceval.c: it's basically a mutable C integer which is used instead of the current Python integer index.
The details can be found in my old patch:
http://starship.python.net/crew/lemburg/mxPython-1.5.patch.gz
Ah, yes, that's what I was thinking of.
""" Generic object iterators. [...]
Thanks -- yes, that's what I was thinking of. Did you just whip this up?
--Guido van Rossum (home page: http://www.pythonlabs.com/~guido/)
- Previous message: [Python-Dev] iterators
- Next message: [Python-Dev] iterators
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]