#8611: speed up cached_function and cached_method
---------------------------+------------------------------------------------
Reporter: jason | Owner: tbd
Type: enhancement | Status: new
Priority: major | Milestone:
Component: misc | Keywords:
Author: Jason Grout | Upstream: N/A
Reviewer: | Merged:
Work_issues: |
---------------------------+------------------------------------------------
Comment(by jason):
BEFORE PATCH:
{{{
sage: def g(i=15):
... return sum(range(2**i))
...
sage: @cached_function
sage: def cached_g(i=15):
... return sum(range(2**i))
...
sage: class A:
... @cached_method
... def decorator_cache(self, i=15):
... return sum(range(2**i))
... def fast_cache(self, i=15):
... try:
... return self._fast_cache
... except AttributeError:
... self._fast_cache=sum(range(2**i))
... return self._fast_cache
sage: timeit('g()')
125 loops, best of 3: 2.02 ms per loop
sage: timeit('cached_g()')
625 loops, best of 3: 37.9 µs per loop
sage: timeit('cached_g()')
625 loops, best of 3: 41.8 µs per loop
sage: c=A()
sage: timeit('c.decorator_cache()')
625 loops, best of 3: 64.8 µs per loop
sage: timeit('c.fast_cache()')
625 loops, best of 3: 1.06 µs per loop
}}}
AFTER PATCH
{{{
sage: def g(i=15):
... return sum(range(2**i))
...
sage: @cached_function
sage: def cached_g(i=15):
... return sum(range(2**i))
...
sage: class A:
... @cached_method
... def decorator_cache(self, i=15):
... return sum(range(2**i))
... def fast_cache(self, i=15):
... try:
... return self._fast_cache
... except AttributeError:
... self._fast_cache=sum(range(2**i))
... return self._fast_cache
sage: timeit('g()')
125 loops, best of 3: 2.94 ms per loop
sage: timeit('cached_g()')
625 loops, best of 3: 1.64 µs per loop
sage: c=A()
sage: timeit('c.decorator_cache()')
625 loops, best of 3: 22 µs per loop
sage: timeit('c.fast_cache()')
625 loops, best of 3: 678 ns per loop
}}}
--
Ticket URL: <http://trac.sagemath.org/sage_trac/ticket/8611#comment:1>
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.