>>> What I can see is that flatten spends considerable amounts of time in >>> things >>> which recursively call flatten *again* (i.e. things like a *= b). This >>> seems >>> to indicate that it can probably benefit from memoization caching. >> >> >> Or it might be simpler and as effective to have this >> half-baked-memoization: >> >> def flatten(expr): >> return _flatten(expr, set())[0] >> >> def _flatten(expr, already_flattened): >> for subexpr in expr: >> if subexpr in already_flattened: >> do nothing >> else: >> add flattened_subexpr to already_flatten >> >> It does not memorize results, it memorizes what is already flat. And >> it is the local type of memoization, but that is another topic. >> >> Anyway, I am just making some noise, this is probably not any better >> than usual memoization. >> > > Why is this better?
Because, depending on how/when you rebuild the tree, you might be able to skip some rebuilds (without jumping through equality checks. etc). But this is very much a micro-optimization that might not even work, so I guess it does not need to be discussed now. Sorry about mentioning it. -- You received this message because you are subscribed to the Google Groups "sympy" group. To unsubscribe from this group and stop receiving emails from it, send an email to sympy+unsubscr...@googlegroups.com. To post to this group, send email to sympy@googlegroups.com. Visit this group at http://groups.google.com/group/sympy?hl=en-US. For more options, visit https://groups.google.com/groups/opt_out.