[Cython] __next__ | not being able to use cpdef
Stefan Behnel
stefan_ml at behnel.de
Sun Dec 7 08:39:59 CET 2008
Hi,
Jelle Feringa wrote:
> cdef class FastLoopObj1d:
>
> cdef int N, i
> cdef object obj
>
> def __init__(self, n, obj ):
> self.N = n-1
> self.i = 0
> self.obj = obj
>
> def __iter__(self):
> return self
>
> # no cpdef allowed?
> def __next__( self ):
> if self.i < self.N:
> self.i+=1
> return self.obj[self.i]
> else:
> raise StopIteration
>
> I noticed that the cython iterator is about twice as fast compared to
> the python version, when indexing an iterable object.
> A respectable speedup, given how trivial the implementation is. Would
> you consider the implementation optimal? Is this the fastest way of
> indexing an iterable object?
This looks a bit like a stripped down version of itertools.islice() to me.
Have you tried that instead?
Stefan
More information about the Cython-dev
mailing list