I was studying about cell call recurrence and mentioned some documents that came in. Sun Java does not apply tail call optimization. I have written the following codes to calculate Fibonacci numbers in 3 different ways: 1. Erectorative 2. Head recursive 3. Tail recursive
Public class Fibonacci {public static zero main (string [] args throws an interrupted expression {int n = integer. Pierce (Argos [0]); System.out.println ("\ nn's value:" + n); System.out.println ("Using \ nchanges:"); Long L1 = System Nano time (); FibonacciIterative (n); Long time l2 = System.nanoTime (); System.out.println ("Running Time =" + (L2 - L1)); Println (fibonacciIterative (n)); System.out.println ("Use of Tail Riction:"); Long L3 = System Nano time (); FibonacciTail (n); Long L4 = System Nano time (); System.out.println ("tail recursive time =" + (L4 - L3)); Println (fibonacciTail (n)); System.out.println ("Using \ n Recurring:"); Long L5 = system Nano time (); FibonacciRecursive (n); Long time l6 = System.nanoTime (); System.out.println ("Head recurring time =" + (L6 - L5)); } Private static long fibonacciquery (int no) {if (num == 0) {return 0L; } If (num == 1) {return 1L; } Return fibonacci reverse (num-1) + Fibonacci reverse (number -2); } Private static long Fibonacciarable (Int n) feet foot split interrupted expression (long [] ARR = New Long [N + 1]; Advent [0] = 0; Advent [1] = 1; (Int i = 2; i & lt; = n; i ++) {// thread Sleep (1); Arr [i] = arr [i - 1] + ARR [i - 2]; } Return on arrival [N]; } Private Stable Long Fibonacetel (Int N) {if (n == 0) returns; Returns Fib Helper (N, 1, 0, 1); } Private Static Long Fib Helper (Int N, Int M, Longer FIBM_Manice_On, Longer FIBM) {If (N = M) Returns FIBM; Returns Fib Helper (N, M + 1, FIBM, FIBM_mins_on + FIBM); }} On the run of this program, I removed some results:
- Head recursive method n & gt; 50 is not finished for the program Anyone seen as hanging, why can it be so?
- Compared to major recurrence, the tail recursive method has taken a lot of time Sometimes it has taken less time than an entererative method This means that Java is some of the tail call optimization Does it internally? And if this happens then I stack it overflow error n & gt; 5000?
System specs:
Intel Core Processor,
Windows XP,
32 bit Java 1.6
Default stack size for JVM
Does this mean that Java does something Tail call optimization internally?
No, it does not apply hotspot JIT compiler tail call customization.
The results you are seeing are the specifications of the errors that you see in the Java Benchmark which does not take account of JVM warmup, for example, "first few times" is called a method , It will be executed by the interpreter, then the JIT compiler will compile the method ... and it will get faster.
To achieve meaningful results, insert a loop around the entire loop and run it several times until the time remains constant. Then remove the results from early iterations.
... I call it stack overflow error by n & gt; But why? 5000?
It is just proof that tail calls are not being optimized.
Comments
Post a Comment