On Wed, Apr 15, 2009 at 2:31 AM, Robert Bradshaw
<[email protected]> wrote:
> On Apr 5, 2009, at 12:26 PM, Hoyt Koepke wrote:
>
>> Hello,
>>
>> I'm using an extension class where I need to create a number of
>> instances quickly, and I'm looking at the section "Can Cython create
>> objects or apply operators to locally created objects as pure C code?"
>> on http://wiki.cython.org/FAQ.  Is this information still correct?
>>
>> In particular, I'm wondering about it in reference to supporting
>> cyclical garbage collection and creating object via PyObject_GC_New
>> etc.  IIRC, cython automatically supports cyclical garbage collection
>> on classes that have python members, and my extension class falls into
>> this category.  Should I
>>
>> a) Use Py_NEW, as specified in the FAQ?
>
> Yes, if one has an absolute need for speed. Cyclic garbage collection
> is supported via a flag on the type.
>
>> b) Replace Py_NEW with Py_Object_GC_New, and do a?
>
> Py_Object_GC_New will allocate the space, but will not call the
> __cinit__ functions, and in particular will not initialize the base
> fields, so don't use this unless you really know what you're doing.
>
>> c) Ignore this and just go with standard creation and deletion,
>> ignoring the fact that I'll be calling the constructors through
>> python.
>
> This is what I would do, at least until I determined object creation
> was a large bottleneck. PY_NEW as stated on the wiki calls the
> __cinit__ functions, but avoids calling the __init__ functions, so
> depending on your usecase it can be a savings. Also, it would be
> desirable and feasible to potentially call __init__ via a C call
> rather than a Python call (maybe, if the signature is simple enough,
> using the cpdef trick to avoid the pythonic argument packing/
> unpacking overhead.)
>
> - Robert
>
> _______________________________________________
> Cython-dev mailing list
> [email protected]
> http://codespeak.net/mailman/listinfo/cython-dev
>

hi, i have a use-case where i'm creating lots of objects as well. so i
wrote up a quick test to see the speed of various methods:
http://gist.github.com/95916

assuming i haven't done anything stupid, results in seconds for that case are:

PY_NEW on Cython class 0.652909994125
__init__ on Python class 1.51110291481
batch PY_NEW 0.434900999069
__init__ on Cython class 0.673034906387
batch __init__ on Cython class 0.532235145569

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

Reply via email to