fibonacci algorithm using BigInteger

I'm not quite sure how to write a timer.java. that times the three algorithms for n up to 1000. Also I need to time the two fastest algorithms up to the given limit. Time fibo1 for increasing values of n until the waiting time becomes unreasonable (order of minutes).

This timer needs to output a table of the form

0 t1(0) t2(0) t3(0)

1 t1(1) t2(1) t3(1)

2 t1(2) t2(2) t3(2)

...

n t1(n) t2(n) t3(n)

ti(n) is the time of fiboi on input n.

here's my fibonacci.java

/*

import java.math.BigInteger;

import javax.swing.*;

import javax.swing.event.*;

import java.math.BigInteger;

public class Fibonacci

{

/*BigInteger fibo1( int k );

BigInteger fibo2( int k );

BigInteger fibo3( int k );*/

public BigInteger fibo1( int k )

{

if( k == 0 || k == 1)

return k;

else

return fibo1(k-1) + fibo1(k-2);

}

public BigInteger fibo2( int k )

{

BigInteger j[] = new BigInteger [k+1];

for( int i=0; i<=k; i++ )

j[]=-1; j[0]=0; f[1]=1;

return fibo(j,k);

}

int fibo( int j[], int k)

{

if( f[n] >= 0)

return j[k];

if( k==0 || k==1 )

return f[k]=k;

else

{

j[k]=fibo(f, k-1) + fibo(f, k-2);

return j[k];

}

}

public BigInteger fibo3 (int k)

{

BigInteger f0 = BigInteger.ZERO;

BigInteger f1 = BigInteger.ONE;

BigInteger temp;

if( k==0 )

return f0;

else if ( k==1 )

return f1;

for( int k=1; i<=k; i++ )

{

BigInteger f2=f0.add(f1);

f0=f1;

f1=f2;

}

return f1;

}

}

*/

[1738 byte] By [silentevoa] at [2007-11-26 14:38:13]
# 1
Start simple - figure out how to time an interval, first - then you can add to it.
ChuckBinga at 2007-7-8 8:19:15 > top of Java-index,Java Essentials,New To Java...
# 2
See http://java.sun.com/j2se/1.5.0/docs/api/java/lang/System.html
YAT_Archivista at 2007-7-8 8:19:15 > top of Java-index,Java Essentials,New To Java...
# 3

The system class has a millisecond current time method, and a nanosecond current time method.

To figure out the time that has elapsed over an interval, determine the current time at the start of the interval, and then again at the end of the interval.

Then subtract the time before and after, to be left with the elapsed number of milliseconds (or nanoseconds).

It's as easy as that.

Next, the trick is knowing how to use the times once you have them.

If you are planning on comparing the speeds of each method, you will want to repeat the measurements several times (possibly on different computers), and you will want to be aware of the work of other processes being performed on your processor -> You java VM does not have 100% share of time in your processor, and the share of time it has changes almost arbitrarily.

You will want to perform the comparison enough times under enough different conditions to be sure that one is, in fact, better than the other.

etc.

- Adam

guitar_man_Fa at 2007-7-8 8:19:15 > top of Java-index,Java Essentials,New To Java...
# 4
by the way, if you want to time each algorithm, you will have to perform the algorithms separately, and then display all of the results at the end, so you will need to store the results in arrays until you are ready to display the results to the console.
guitar_man_Fa at 2007-7-8 8:19:15 > top of Java-index,Java Essentials,New To Java...