[Cython] __next__ | not being able to use cpdef

Robert Bradshaw robertwb at math.washington.edu
Mon Dec 8 20:34:38 CET 2008


On Dec 7, 2008, at 10:37 AM, Aaron DeVore wrote:

> Out of curiosity, would most of the time be spent on the line "return
> self.obj[self.i]"? It seems like a __getitem__ call would be much
> slower than anything else in the class.

Yes, almost certainly.

>
> -Aaron
>
> On Sat, Dec 6, 2008 at 11:39 PM, Stefan Behnel  
> <stefan_ml at behnel.de> wrote:
>> 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
>>
>> _______________________________________________
>> Cython-dev mailing list
>> Cython-dev at codespeak.net
>> http://codespeak.net/mailman/listinfo/cython-dev
>>
> _______________________________________________
> Cython-dev mailing list
> Cython-dev at codespeak.net
> http://codespeak.net/mailman/listinfo/cython-dev



More information about the Cython-dev mailing list