On 03/25/2010 08:49 PM, William Stein wrote:
Hi,

I was trying to understand why certain modular symbols code was too
slow, and ran across a method that looked like this (in
modular/modsym/element.py):

     def modular_symbol_rep(self):
         try:
             return self.__modular_symbols
         except AttributeError:
             A = self.parent()
             v = self.manin_symbol_rep()
             if v == 0:
                 return v
             w = [c * x.modular_symbol_rep() for c, x in v]
             return sum(w)
         return self.__modular_symbols

Of course, on multiple calls, the above is slow, because the
__modular_symbols cache never gets set.
So I though, ah ha!, this is a great example where I should use
@cached_method, now that we have it.
So I tried that, but doing

  timeit('...')

showed that it still took about 20microsend/call to call the above function.
Replacing @cached_method with the try/except as above, but actually
settin gself.__modular_symbols correctly resulted in the speedup I
wanted (e.g., each call takes a few hundred nanoseconds).

MORAL: If you care about speed, don't use the cached_method decorator
yet.   It entails massive overheard.



See #8611 for the start of a patch which speeds up cached_function by about 30x in your case (no arguments), and @cached_method by about 3x. The ticket has before/after timing information.

I think someone more familiar with Python method decorators (in particular, when you have an instance, etc.) could probably speed up the @cached_method a lot more. For example, inst.__dict__.setdefault gets called on every call right now.

@cached_method still isn't nearly as fast as a simple cache like what you have above (see the ticket for details), but it's a start.

Thanks,

Jason

--
To post to this group, send an email to sage-devel@googlegroups.com
To unsubscribe from this group, send an email to 
sage-devel+unsubscr...@googlegroups.com
For more options, visit this group at http://groups.google.com/group/sage-devel
URL: http://www.sagemath.org

To unsubscribe from this group, send email to sage-devel+unsubscribegooglegroups.com or 
reply to this email with the words "REMOVE ME" as the subject.

Reply via email to