Gwion Preprocessor
Memoization
You can use the memoize
pragma to enable memoization on functions:
normal version
fib_recurs.gw
fun int recursive_fib(int n) {
if (n < 2)
return n;
return recursive_fib(n - 2) + recursive_fib(n - 1);
}
<<< 40 => recursive_fib >>>;
memoized version
The syntax of the memoize
pragma is as follow:
#pragma memoization <number of results to store>
See the memoized version of previous function:
fib_recurs_memoize.gw
fun int recursive_fib(int n) {
#pragma memoize 2
if (n < 2)
return n;
return recursive_fib(n - 2) + recursive_fib(n - 1);
}
<<< 40 => recursive_fib >>>;
Under circomstance where memoization is applicable, such as this one, you can see a huge speed-up.
normal:
memoized:
Memoization setting will be active until the end of file or until it is changed. Therefore, if you want to disable memoization for subsequent functions, use:
#pragma memoize 0