Note that there are some explanatory texts on larger screens.

plurals
  1. POAndroid Fibonacci Benchmark / Deep recursion
    text
    copied!<p>I'm running a Fibonacci benchmark on my Android phone and I'm getting some strange results. As I don't really care if the UI-thread is locked or not, I'm running the code below inside the UI-thread in my application (could this affect the performance btw?).</p> <pre><code>public void startBenchmark(View view) { results = ""; results += String.format("Begin test"); for (int i = 45; i &lt; 46; i++) { startTime = System.currentTimeMillis(); fib(i); results += String.format("%d\n", System.currentTimeMillis() - startTime); } results += String.format("End test"); Log.d("Results", results); Log.d("Status", "Finished"); } private static int fib(int n) { return n &lt;= 1 ? n : fib(n - 1) + fib(n - 2); } </code></pre> <p>I've also implemented the corresponding code in JavaScript; </p> <pre><code>function performBenchmark() { for (var i = 45; i &lt; 46; i++) { benchmark(i) } } function benchmark(n){ var start= Date.now(); document.getElementById("disp").innerHTML += "fib(" + n + "): " + fib(n) + " &lt;br /&gt;"; document.getElementById("results").innerHTML += (Date.now() - start) + "&lt;br /&gt;"; } function fib(n) { return n &lt;= 1 ? n : fib(n - 1) + fib(n - 2); } </code></pre> <p>My problem is that for fib(45) I get something like 420 seconds on the native platform using Java and 120 seconds using Javascript in Chrome, both running on my Samsung Galaxy Nexus. </p> <p>Is there something blatantly wrong with my implementation in Java for Android that could be slowing down the benchmark?</p> <p>NOTE; <strong>I'm not primarily looking to switch to a faster algorithm but I'm trying to understand why Javascript (and also an implementation I made for iOS) is much faster than the implementation in Java for Android.</strong> </p> <p>Running on my laptop I get much faster results for Java than for Javascript.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload