[REBOL] Two implementations...
From: rgombert::essentiel::net at: 5-Feb-2003 18:24
Hi all rebolers,
I'm back to rebol after two year, and have great times playing with it ;-)
I wanted to show some optimisation possible for some recursive computation,
and wrote the small program below.
Run the test, it talks for itself...
I know this is nothing new for most of you... but maybe it can be of some
help for someone ;-)
Have fun with it...
############### CODE ###############
REBOL [
title: "Test for two implementation of FIBONACCI function"
author: "Renaud GOMBERT"
notes: { slowfib use the intuitive way of implementing fib(n). fastfib
do the same,
but use a block to store allready known values of fibo(n).
* As fastfib store all results, it is provided with a refinement to
release them.
* Both functions works for positive integers.
* Max calculable value is fib(46), according to Rebol math
capabilities.
* If you want to waste CPU time, try running slowfib 46 LOL ;-)
}
]
krono: func [code n /local T][
prin [code "running" n "times... "]
T: now/time/precise
print ["and return:" loop n [do code] ", taking:" (now/time/precise -
T)]
]
slowfib: func [n][
either n < 2 [n][add slowfib n - 2 slowfib n - 1]
]
fastfib: function [n /release][r][
accu: [1 1]
r: either n <= length? accu [
accu/:n
][
last append accu add fastfib n - 2 fastfib n - 1
]
if release [clear at accu 3]
r
]
; TEST RUN
do [
print "TESTING THE TWO FIB FUNCTIONS :^/"
krono [slowfib 25] 10
krono [fastfib 25]10
krono [fastfib/release 25]10
krono [slowfib 30] 5
krono [fastfib 30] 5
krono [fastfib/release 30] 5
krono [fastfib 46] 10000
krono [fastfib/release 46] 10000
krono [fastfib 46] 1450000
krono [slowfib 11] 10000
]
halt
############### END OF CODE ###############
---------------------
Renaud GOMBERT - www.essentiel.net