#11115: Rewrite cached_method in Cython
---------------------------+------------------------------------------------
Reporter: SimonKing | Owner: jason
Type: enhancement | Status: needs_review
Priority: major | Milestone: sage-4.7
Component: misc | Keywords: category cython cache
Author: Simon King | Upstream: N/A
Reviewer: | Merged:
Work_issues: |
---------------------------+------------------------------------------------
Comment(by SimonKing):
Here is another data point:
{{{
sage: class MyClass:
....: def __init__(self, v):
....: self.__v = v
....: def v(self):
....: return self.__v
....: @cached_method
....: def cached_v(self):
....: return self.__v
....: def derived_from_underscore(self):
....: x = self.__v^2
....: def derived_from_cache(self):
....: x = self.cached_v()^2
....:
sage: O = MyClass(100)
sage: O.v()
100
sage: O.cached_v()
100
sage: timeit('O.v()')
625 loops, best of 3: 599 ns per loop
sage: timeit('O.cached_v()')
625 loops, best of 3: 425 ns per loop
}}}
There are methods that are frequently called and just return a double-
underscore attribute. It would be faster to achieve the same with a cached
method! I guess this is because of the name mangling that takes place for
double-underscore attributes.
However, that only holds when calling a method. Inside of a method, the
double-underscore seems to be quicker:
{{{
sage: timeit('O.derived_from_underscore()')
625 loops, best of 3: 1.65 µs per loop
sage: timeit('O.derived_from_cache()')
625 loops, best of 3: 1.98 µs per loop
}}}
I think it would be worth trying to use this trick: Little methods
returning a double-underscore attribute appear all over the place. See
#9138 for one example.
--
Ticket URL: <http://trac.sagemath.org/sage_trac/ticket/11115#comment:7>
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.