#8611: speed up cached_function and cached_method
---------------------------------------+------------------------------------
   Reporter:  jason                    |       Owner:  tbd          
       Type:  enhancement              |      Status:  needs_review 
   Priority:  major                    |   Milestone:  sage-4.6.1   
  Component:  misc                     |    Keywords:  cached method
     Author:  Jason Grout, Simon King  |    Upstream:  N/A          
   Reviewer:                           |      Merged:               
Work_issues:                           |  
---------------------------------------+------------------------------------

Comment(by SimonKing):

 I forgot to provide a comparison against an explicitly coded cache:
 {{{
 sage: class Foo:
 ....:     @cached_method
 ....:     def decorator_bar(self, n=15):
 ....:         return sum(range(2**n))
 ....:     def manual_bar(self, n=15):
 ....:         try:
 ....:             D = self._my_cache
 ....:         except AttributeError:
 ....:             D = self._my_cache = {}
 ....:             D[n] = sum(range(2**n))
 ....:         return D[n]
 ....:
 sage: F = Foo()
 sage: F.decorator_bar() is F.decorator_bar()
 True
 sage: F.decorator_bar() == F.manual_bar()
 True
 sage: F.manual_bar() is F.manual_bar()
 True
 sage: timeit('F.manual_bar()', number=10000)
 10000 loops, best of 3: 1.02 µs per loop
 sage: timeit('F.decorator_bar()', number=10000)
 10000 loops, best of 3: 1.71 µs per loop
 sage: timeit('F.decorator_bar(15)', number=10000)
 10000 loops, best of 3: 3.05 µs per loop
 }}}

 Hence, the new version of cached methods can almost compete with an
 explicitly coded cache, but is of course much more comfortable for the
 programmer.

-- 
Ticket URL: <http://trac.sagemath.org/sage_trac/ticket/8611#comment:9>
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 post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/sage-trac?hl=en.

Reply via email to