Fibonacci and performance

Tony Kimball alk@pobox.com
Fri Apr 27 09:58:00 GMT 2001


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");
	}
}


More information about the Java mailing list

AltStyle によって変換されたページ (->オリジナル) /