factor/apps/benchmarks/fib.factor

67 lines
1.2 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
2006-05-02 00:11:59 -04:00
USE: words
! Five fibonacci implementations, each one slower than the
! previous.
2006-11-09 02:02:14 -05:00
: fast-fixnum-fib ( m -- n )
dup 1 fixnum<= [
drop 1
] [
1 fixnum-fast dup fast-fixnum-fib
swap 1 fixnum-fast fast-fixnum-fib fixnum+fast
] if ;
[ 9227465 ] [ 34 fast-fixnum-fib ] unit-test
2006-11-09 02:02:14 -05:00
: fixnum-fib ( m -- n )
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 ;
[ 9227465 ] [ 34 fixnum-fib ] unit-test
2004-10-27 21:21:31 -04:00
2006-11-09 02:02:14 -05:00
: fib ( m -- n )
2006-05-02 00:11:59 -04:00
dup 1 <= [ drop 1 ] [ dup 1 - fib swap 2 - fib + ] if ;
[ 9227465 ] [ 34 fib ] unit-test
2005-05-09 02:34:15 -04:00
TUPLE: box i ;
2006-11-09 02:02:14 -05:00
: tuple-fib ( m -- n )
2005-05-09 02:34:15 -04:00
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>
] if ;
2005-05-09 02:34:15 -04:00
[ T{ box f 9227465 } ] [ T{ box f 34 } tuple-fib ] unit-test
SYMBOL: n
2006-11-09 02:02:14 -05:00
: namespace-fib ( m -- 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 ;
[ 1346269 ] [ 30 namespace-fib ] unit-test