#14214: Cythoned homsets
----------------------------------------------+-----------------------------
       Reporter:  SimonKing                   |         Owner:  tbd             
                 
           Type:  enhancement                 |        Status:  needs_work      
                 
       Priority:  major                       |     Milestone:  sage-5.10       
                 
      Component:  performance                 |    Resolution:                  
                 
       Keywords:  Hom, cython, cached method  |   Work issues:  How to store 
domain and codomain?
Report Upstream:  N/A                         |     Reviewers:                  
                 
        Authors:  Simon King                  |     Merged in:                  
                 
   Dependencies:  #14159, #12951, #13184      |      Stopgaps:                  
                 
----------------------------------------------+-----------------------------

Comment (by nbruin):

 A very rough implementation:
 {{{
 from sage.misc.lazy_attribute import lazy_attribute

 cdef class LazyAttribute:
     cdef public f
     cdef public str __name__
     def __init__(self,f):
         self.f=f
         self.__name__=f.__name__
     def __get__(self,instance,cls):
         if instance is None:
             return lazy_attribute(self.f)
         v=self.f(instance)
         setattr(instance,self.__name__,v)
         return v
 }}}
 Note I'm just reusing the old `lazy_attribute` here to deal with
 documentation issues.
 {{{
 sage: class T(object):
 ...       @LazyAttribute
 ...       def t(self):
 ...           """docstring of f"""
 ...           return 1000
 ...       @lazy_attribute
 ...       def s(self):
 ...           """docstring of f"""
 ...           return 1000
 sage: timeit("T().t",repeat=10,number=10000)
 10000 loops, best of 10: 374 ns per loop
 sage: timeit("T().s",repeat=10,number=10000)
 10000 loops, best of 10: 2.93 µs per loop
 }}}
 If I just copy over the `__get__` method from the original
 `lazy_attribute` the difference is a little less pronounced, because that
 code does a lot more. After changing `self.f.__name__` to `self.__name__`
 I'm getting `1.2 µs`, so that still more than twice as fast.

-- 
Ticket URL: <http://trac.sagemath.org/sage_trac/ticket/14214#comment:50>
Sage <http://www.sagemath.org>
Sage: Creating a Viable Open Source Alternative to Magma, Maple, Mathematica, 
and MATLAB

-- 
You received this message because you are subscribed to the Google Groups 
"sage-trac" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/sage-trac?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.


Reply via email to