This is the mail archive of the
java@gcc.gnu.org
mailing list for the Java project.
Fibonacci and performance
- To: tromey at redhat dot com
- Subject: Fibonacci and performance
- From: Tony Kimball <alk at pobox dot com>
- Date: Fri, 27 Apr 2001 11:58:24 -0500 (CDT)
- Cc: java at gcc dot gnu dot org
- References: <874rval8r6.fsf@creche.redhat.com>
- Reply-To: alk at pobox dot com
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");
}
}