Hi,

now, those are interesting timings. Which versions of Cython and Python did
you use?

Brent Pedersen wrote:
> here are the new timings:
> 
> PY_NEW on Cython class: 1.137
> __init__ on Cython class 1.154

You're right, that's almost negligible. However, the __init__ benchmark
involves the overhead of a Python call to create_interval, which uses the
same Python calling convention as the constructor. So it's more or less
doing the same thing in both cases, just additionally calling __init__
through a C struct member dereference in the second case (but without
repacking the arguments).

Also note that it can actually be a feature that PY_NEW doesn't call
__init__(). I happily misuse that in lxml.etree to assign different
behaviour to a user class instantiation and an internal proxy creation.


> batch PY_NEW total: 0.821 , interval only: 0.363
> batch __init__ on Cython class total 0.975 , interval_only: 0.524

As expected, the difference is a lot larger here. Ignoring the setup
overhead, that's some 30% faster, as PY_NEW does the field initialisation
inside a plain C call, whereas the call to __init__ requires the Python
calling convention (i.e. tuple packing and unpacking).


> __init__ on Python class: 28.468
> __init__ on Python class with slots: 9.936

Ok, that's a completely different performance league. The first benchmark
requires setting up a dictionary for the instance and putting the
attributes there, whereas the second happily assigns the attributes to
predefined slots, without the overhead of a dict.

To sum it up, using PY_NEW from the beginning looks like a premature
optimisation to me. If you notice that class creation becomes a bottleneck
later on, *and* your classes are instantiated internally in the relevant
cases, *and* the setup involves working with plain C field values (such as
the 'start' and 'end' ints in your case), switching to PY_NEW can give you
about 30% in plain class instantiation time (YMWV, don't forget to
benchmark your own code).

Thanks for sharing,

Stefan
_______________________________________________
Cython-dev mailing list
[email protected]
http://codespeak.net/mailman/listinfo/cython-dev

Reply via email to