On 12/11/2010 03:00 AM, Stefan Behnel wrote: > Hi, > > here's a little generator benchmark: > > ====================== > def cgen(a,b): > while 1: > yield a+b > > d = {} > exec ''' > def pygen(a,b): > while 1: > yield a+b > ''' in d > > pygen = d['pygen'] > > > def repeat_gen(gen, int count): > cdef int i > for i in range(count): > next(gen) > try: > gen.throw(GeneratorExit) > except GeneratorExit: > pass > ====================== > > timeit gives me this in Py3.2: > > $ python3 -m timeit -s 'from genbench import repeat_gen, cgen, pygen' > 'repeat_gen(pygen(1,2), 10000)' > 1000 loops, best of 3: 827 usec per loop > > $ python3 -m timeit -s 'from genbench import repeat_gen, cgen, pygen' > 'repeat_gen(cgen(1,2), 10000)' > 1000 loops, best of 3: 271 usec per loop > > > And here's another one where we add Cython's looping advantage: > > ====================== > def cgen(a,b): > cdef int i > for i in range(a): > yield b > > d = {} > exec ''' > def pygen(a,b): > for i in range(a): > yield b > ''' in d > > pygen = d['pygen'] > > > def repeat_gen(gen): > for _ in gen: > pass > ====================== > > $ python3 -m timeit -s 'from genbench import repeat_gen, cgen, pygen' > 'repeat_gen(pygen(10000, 2))' > 1000 loops, best of 3: 881 usec per loop > $ python3 -m timeit -s 'from genbench import repeat_gen, cgen, pygen' > 'repeat_gen(cgen(10000, 2))' > 10000 loops, best of 3: 116 usec per loop > > > That's not too bad for a start. >
This is all very cool! When it comes to performance, I want to point out that there seem to be quite a lot of libraries (of the "works only on some platforms" type) implementing real coroutines in C. So for really pushing generator performance, perhaps we could use one of those. Keeping multiple C stacks around is probably always going to be faster than unpacking/repacking variables. As long as a "fast enough" implementation is available on all platforms, perhaps it will suffice to only support the most common platforms "lightning fast". Dag Sverre _______________________________________________ Cython-dev mailing list Cython-dev@codespeak.net http://codespeak.net/mailman/listinfo/cython-dev