(blog ‘lucindo)

um dia eu aprendo a programar

Lisp Hacks

Link com algumas coisas legais: Lisp hacks.

Um dos possíveis hacks é a memoização de funções. Fiz um pequeno teste:

CL-USER(1): (requireasdf-install)
…
CL-USER(2): (asdf-install:install :memoize)
…
CL-USER(3): (defun fib (n)
              (if (< n 2)
                  n
                  (+ (fib (- n 1))
                     (fib (- n 2)))))
FIB
CL-USER(4): (time (fib 42))

Evaluation took:
  21.129 seconds of real time
  21.04396 seconds of user run time
  0.025874 seconds of system run time
  0 calls to %EVAL
  0 page faults and
  0 bytes consed.
267914296
CL-USER(5): (compile ‘fib)

FIB
NIL
NIL
CL-USER(6): (time (fib 42))

Evaluation took:
  20.074 seconds of real time
  19.975544 seconds of user run time
  0.03109 seconds of system run time
  0 calls to %EVAL
  0 page faults and
  0 bytes consed.
267914296
CL-USER(7): (memoize:memoize-function ‘fib)

FIB
CL-USER(8): (time (fib 42))

Evaluation took:
  0.0 seconds of real time
  7.1e-5 seconds of user run time
  2.e-6 seconds of system run time
  0 calls to %EVAL
  0 page faults and
  8,136 bytes consed.
267914296


 | Enviar por e-mail  | Hits para esta publicação: 369

Deixe uma resposta.