[sage-devel] Re: Status of @lazy_attribute for cdef classes
Hi Jeroen, On 2017-08-25, Jeroen Demeyerwrote: > On 2017-08-25 13:37, Simon King wrote: >> Not much, actually: > > My point was that a __dict__ makes any attribute access slower, not just > this lazy attribute. It will also seriously slow down cpdef methods. OK, if it concerns *all* cpdef methods then I understand that one should be reluctant. @property supported by a cdef attribute works better anyway. Cheers, Simon -- You received this message because you are subscribed to the Google Groups "sage-devel" group. To unsubscribe from this group and stop receiving emails from it, send an email to sage-devel+unsubscr...@googlegroups.com. To post to this group, send email to sage-devel@googlegroups.com. Visit this group at https://groups.google.com/group/sage-devel. For more options, visit https://groups.google.com/d/optout.
Re: [sage-devel] Re: Status of @lazy_attribute for cdef classes
On 2017-08-25 13:37, Simon King wrote: Not much, actually: My point was that a __dict__ makes any attribute access slower, not just this lazy attribute. It will also seriously slow down cpdef methods. -- You received this message because you are subscribed to the Google Groups "sage-devel" group. To unsubscribe from this group and stop receiving emails from it, send an email to sage-devel+unsubscr...@googlegroups.com. To post to this group, send email to sage-devel@googlegroups.com. Visit this group at https://groups.google.com/group/sage-devel. For more options, visit https://groups.google.com/d/optout.
[sage-devel] Re: Status of @lazy_attribute for cdef classes
Hi Jeroen, On 2017-08-25, Jeroen Demeyerwrote: > One workaround is adding a __dict__: > ... > This does make everything slower though, so I guess it's not what you want. Not much, actually: sage: cython(""" : from sage.structure.element cimport Element : cdef class CyElement(Element): : cdef object _test : def __init__(self, P): : Element.__init__(self,P) : self.test = 15 : @property : def test(self): : return self._test : @test.setter : def test(self, x): : self._test = x : from sage.misc.lazy_attribute import lazy_attribute : cdef class CyElement2(Element): : cdef dict __dict__ : @lazy_attribute : def test(self): : return 15 : """) sage: ec = CyElement(ZZ) sage: ec2 = CyElement2(ZZ) sage: %timeit ec.test 1000 loops, best of 3: 41 ns per loop sage: %timeit ec2.test 1000 loops, best of 3: 45.4 ns per loop That's even a little faster than attribute access in Python (a lazy attribute on a Python class defined in a Cython file takes 51.4 ns per loop). Thank you for that trick! Best regards, Simon -- You received this message because you are subscribed to the Google Groups "sage-devel" group. To unsubscribe from this group and stop receiving emails from it, send an email to sage-devel+unsubscr...@googlegroups.com. To post to this group, send email to sage-devel@googlegroups.com. Visit this group at https://groups.google.com/group/sage-devel. For more options, visit https://groups.google.com/d/optout.
Re: [sage-devel] Re: Status of @lazy_attribute for cdef classes
On 2017-08-25 13:07, Simon King wrote: Hi! I just realise that @property works for Cython, and it is competitive! Indeed. Cython has optimized code to deal with standard decorators like @staticmethod, @classmethod and @property. The reason that lazy_import does not work for Cython classes is just that arbitrary attribute access is not allowed, i.e. obj.test = 15 doesn't work, even if "test" is not a lazy attribute. One workaround is adding a __dict__: cdef class MyElement(Element): cdef dict __dict__ @lazy_attribute def test(self): return 15 This does make everything slower though, so I guess it's not what you want. I don't think there is a way to make @lazy_attribute work for types without a __dict__ (apart from specifically patching Cython to support @lazy_attribute). -- You received this message because you are subscribed to the Google Groups "sage-devel" group. To unsubscribe from this group and stop receiving emails from it, send an email to sage-devel+unsubscr...@googlegroups.com. To post to this group, send email to sage-devel@googlegroups.com. Visit this group at https://groups.google.com/group/sage-devel. For more options, visit https://groups.google.com/d/optout.
[sage-devel] Re: Status of @lazy_attribute for cdef classes
Hi! I just realise that @property works for Cython, and it is competitive! Namely: sage: cython(""" : from sage.structure.element cimport Element : cdef class CyElement(Element): : cdef object _test : def __init__(self, P): : Element.__init__(self,P) : self.test = 15 : @property : def test(self): : return self._test : @test.setter : def test(self, x): : self._test = x : from sage.misc.lazy_attribute import lazy_attribute : class PyElement(Element): : @lazy_attribute : def test(self): : return 15 : """) sage: ep = PyElement(ZZ) sage: ep.test 15 sage: ec = CyElement(ZZ) sage: ec.test 15 sage: %timeit ec.test 1000 loops, best of 3: 40.5 ns per loop sage: %timeit ep.test 1000 loops, best of 3: 51.5 ns per loop So, apparently a property getter in Cython is faster than an attribute lookup in Python. Cheers, Simon -- You received this message because you are subscribed to the Google Groups "sage-devel" group. To unsubscribe from this group and stop receiving emails from it, send an email to sage-devel+unsubscr...@googlegroups.com. To post to this group, send email to sage-devel@googlegroups.com. Visit this group at https://groups.google.com/group/sage-devel. For more options, visit https://groups.google.com/d/optout.