conditional computation
robert
no-spam at no-spam-no-spam.com
Fri Oct 27 15:06:03 EDT 2006
> Robert, that's not the point. I do have enough Python knowledge to
> understand this (totally trivial) code !-)
>
> What I don't understand is how this code is supposed to save you from
> having to actually write both complex-key-expr and
> expensive-calc-expression more than once. You need them both each time
> you access the cache - whether the result of expensive-calc-expression
> has already been cached or not.
>
> Now this seems so obvious that I guess I failed to understand some point
> in your original spec (emphasis is mine):
> """
> I want to use a computation cache scheme like
>
>
> o = CACHECOMPUTE complex-key-expr expensive-calc-expr
>
> frequently and elegantly *without writing complex-key-expr or
> expensive-calc-expr twice*.
> """
But it is written only once.? And I have the cache functionality.
"You need them both each time you access the cache" : What you mean with "need"? I still suppose you think expensive-calc-expr is executed every time. But it is not - only the first time.
In my first ugly solution ...
_=complex-key-expr; o=cache.get(_) or cache.setdefault(_,expensive-calc-expr)
. it is also only executed once - because the part after "or" is only evaluated if cache.get(_) doesn't find it in the cache.
Just ..
o = memo( complex-key-expr, lambda: expensive-calc-expr )
.. is much more elegant.
Contrast to explain: Uncaching (stupid) would be for example:
o = cache.setdefault(complex-key-expr, expensive-calc-expr)
Because expensive-calc-expr is executed every time. (I remember once I had such a speed blocker bug in sloppy written code and only found it with the profiler)
Javaish ugly without writing the expressions twice (thus using variables) would be:
key=complex-key-expr
if key in cache:
o=cache[key]
else:
o=expensive-calc-expr
cache[key]=o
But who wants to write such code with python, when you have to convert many lines in an optimization effort in your code like
store-var = expensive-calc-expr
...
store-var = expensive-calc-expr
store-var = expensive-calc-expr
store-var = expensive-calc-expr
store-var = expensive-calc-expr
...
Thus what is not solved or what would you do better than:
store-var = memo( complex-key-expr, lambda: expensive-calc-expr )
...
store-var = memo( complex-key-expr, lambda: expensive-calc-expr )
store-var = memo( complex-key-expr, lambda: expensive-calc-expr )
store-var = memo( complex-key-expr, lambda: expensive-calc-expr )
store-var = memo( complex-key-expr, lambda: expensive-calc-expr )
....
I think there is no principally better solution. Maybe format it for the art museum with operators to clean ,-brakets ...
store-var = rmemo(complex-key-expr) << lambda: expensive-calc-expr
-robert
More information about the Python-list
mailing list