• Caching (memoization) in (Gauche, Lisp, Scheme) and Python

    From henhanna@gmail.com@21:1/5 to All on Sun Sep 18 09:25:26 2022
    So... for a few days i've been revising this Code (in Gauche / Lisp / Scheme) to make it run faster.. and last night i could improve it enough to give me the result i wanted in 72 minutes or so (on my slow PC at home).


    ( Maybe... within a few months, i'll write the same program in Python .... to see if it runs 10 or 20 times faster. )


    this was the first time i've used Caching (memoization). ----- instead of calculating (at run-time) Factorial(x) and Combination(x,y) millions of times, i made 2 tables in advance... A simple Table-lookup (Vector-ref
    in Scheme) seems 100 -- 1000 times faster.


    One thought i had was... Maybe Python's Factorial(x) and Combination(x,y) (in Numpy ?) are already so fast that... i don't have to do the Caching (memoization) ???



    --- the 1st change i made (to make it run faster) was to rewrite the Recursive Factorial(x) into iterative (tail-recursive) Factorial(x, val) and that one simple change made it (the whole program) noticeably faster.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From duncan smith@21:1/5 to henh...@gmail.com on Sun Sep 18 19:03:27 2022
    On 18/09/2022 17:25, henh...@gmail.com wrote:

    So... for a few days i've been revising this Code (in Gauche / Lisp / Scheme) to make it run faster.. and last night i could improve it enough to give me the result i wanted in 72 minutes or so (on my slow PC at home).


    ( Maybe... within a few months, i'll write the same program in Python .... to see if it runs 10 or 20 times faster. )


    this was the first time i've used Caching (memoization). ----- instead of calculating (at run-time) Factorial(x) and Combination(x,y) millions of times, i made 2 tables in advance... A simple Table-lookup (Vector-
    ref in Scheme) seems 100 -- 1000 times faster.


    One thought i had was... Maybe Python's Factorial(x) and Combination(x,y) (in Numpy ?) are already so fast that... i don't have to do the Caching (memoization) ???



    --- the 1st change i made (to make it run faster) was to rewrite the Recursive Factorial(x) into iterative (tail-recursive) Factorial(x, val) and that one simple change made it (the whole program) noticeably faster.


    Memoization / table lookup will give you a significant speed up as long
    as you're generating enough factorials / binomial coefficients.
    Depending on what you're doing you might find the functions in
    scipy.special useful,

    https://docs.scipy.org/doc/scipy/reference/special.html

    particularly the gammaln and related functions. You will need to install
    scipy unless you've already done so.

    Duncan

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)