factor/library/test/benchmark/fib.factor

58 lines
1.1 KiB
Factor
Raw Permalink Normal View History

IN: temporary
2004-10-27 21:21:31 -04:00
USE: compiler
USE: kernel
2004-08-23 23:27:55 -04:00
USE: math
USE: test
USE: math-internals
USE: namespaces
! Four fibonacci implementations, each one slower than the
! previous.
: fixnum-fib ( n -- nth fibonacci number )
dup 1 fixnum<= [
drop 1
] [
1 fixnum- dup fixnum-fib swap 1 fixnum- fixnum-fib fixnum+
2005-09-24 15:21:17 -04:00
] if ;
compiled
[ 9227465 ] [ 34 fixnum-fib ] unit-test
2004-10-27 21:21:31 -04:00
: fib ( n -- nth fibonacci number )
2005-09-24 15:21:17 -04:00
dup 1 <= [ drop 1 ] [ 1- dup fib swap 1- fib + ] if ;
2004-12-05 23:00:52 -05:00
compiled
2004-08-23 23:27:55 -04:00
[ 9227465 ] [ 34 fib ] unit-test
2005-05-09 02:34:15 -04:00
TUPLE: box i ;
: tuple-fib ( n -- n )
dup box-i 1 <= [
drop 1 <box>
] [
2005-09-16 22:47:28 -04:00
box-i 1- <box>
2005-05-09 02:34:15 -04:00
dup tuple-fib
swap
2005-09-16 22:47:28 -04:00
box-i 1- <box>
2005-05-09 02:34:15 -04:00
tuple-fib
swap box-i swap box-i + <box>
2005-09-24 15:21:17 -04:00
] if ; compiled
2005-05-09 02:34:15 -04:00
[ T{ box f 9227465 } ] [ T{ box f 34 } tuple-fib ] unit-test
SYMBOL: n
: namespace-fib ( n -- n )
[
n set
n get 1 <= [
1
] [
n get 1 - namespace-fib
n get 2 - namespace-fib
+
2005-09-24 15:21:17 -04:00
] if
] with-scope ; compiled
[ 9227465 ] [ 34 namespace-fib ] unit-test