This is the mail archive of the java@gcc.gnu.org mailing list for the Java project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Fibonacci and performance


Quoth Tom Tromey on , 27 April:
: I wonder if inlining the "already initialized" check from
: _Jv_InitClass would help or hurt (due to increased code size).

When I made the fib(x) method not to be static any more,
the time was cut roughly in half:

% ./Fib.static
102334155
Fibonacci: n = 40 took 12253 ms

% ./Fib.private
102334155
Fibonacci: n = 40 took 5869 ms


public class Fib
{
	private int fib(int n) {
		if (n <= 2 ) {
			return 1;
		} else {
			return (fib(n-1) + fib(n-2));
		}
	}

	public static void main(String[] args) {
		Fib f = new Fib();
		final int n = 40;
		long start = System.currentTimeMillis();
		System.out.println(f.fib(n));
		long end = System.currentTimeMillis();
		System.out.println("Fibonacci: n = " + n + " took "+ (end -
								      start) + " ms");
	}
}


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]