[REBOL] Re: Fun with literal blocks!
From: joel:neely:fedex at: 27-Oct-2000 7:44
[rebol-bounce--rebol--com] wrote:
> Hi Joel,
>
[...snip...]
> My purpose here is to non-curmudgeonly present a WYSIWYG approach to
> Rebol programming, as I stated before.
>
> >
> > This can be a handy tool to *GREATLY* improve performance in some
> > cases.
>
> Not at all, see below!
>
Certainly agreed! It only helps performance when:
1) the recursion causes the same argument to be revisited many
times in a single "top-level" use of the function, gaining
performance improvement at each call, or
2) the function is simply (unavoidably) quite expensive to compute,
so that caching can provide performance improvement on subsequent
calls with the same (or smaller) argument(s).
I probably should have been more explicit about that!
> Why not use a WYSIWYG code (code which doesn't try to modify itself
> during its execution, but does everything we need):
>
> fibonacci-cache: copy [1.0]
> fibonacci: function [n [integer!]] [t] [
>[...snip...]
>
That is, of course, a quite valid way to do the memo-izing. It is
certainly less "tricky". My main reason for not doing it that way
is that it puts 2 names in the global namespace for 1 function. I
can think of other cases the natural combination of auxiliary
functions and memo-ization would require even more names.
As I prefer to hide implementation from interface, and avoid any
unnecessary pollution of the global namespace, I would actually
code a memo-ized Fibonacci as an object in most cases.
Fibonacci: make object! [
cache: array/initial 100 none
f: function [n [integer!]] [t] [
...
]
]
> insert/dup tail fibonacci-cache none n - length? fibonacci-cache
>
Thanks for the reminder! That is, of course, better than the
brute-force loop of my original version!
-jn-