Last active
October 2, 2015 23:07
-
-
Save dg/2341908 to your computer and use it in GitHub Desktop.
Fibonacci number benchmark
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// fibonacci(40) takes 2 seconds in Node.js 0.6.14 | |
// http://en.wikipedia.org/wiki/Fibonacci_number | |
function fibonacci(n) { | |
return n < 2 ? n : fibonacci(n-2) + fibonacci(n-1); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// fibonacci(40) takes 60 seconds in PHP 5.4.0 | |
// fibonacci(40) takes 21 seconds in PHP 7 | |
function fibonacci($n) { | |
return $n < 2 ? $n : fibonacci($n-2) + fibonacci($n-1); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# fibonacci(40) takes 42 seconds in Python 3.2.2 | |
def fibonacci(n): | |
return n if n < 2 else fibonacci(n-1) + fibonacci(n-2) | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# fibonacci(40) takes 18 seconds in Ruby 1.9.3 | |
def fibonacci(n) | |
n < 2 ? n : fibonacci(n-1) + fibonacci(n-2) | |
end |
@jsuchal to zní, jako by jsi chtěl říct, že použitý algoritmus není správný, nebo je snad dokonce hovadina. Nedává snad správné výsledky?
@dg Algoritmus je správny (ten vzorec si vieš vypočítať a overiť na papieri). Problém, ktorý vznikol je realizácia pre čísla s konečnou presnosťou. A áno, nedáva to teda pre vyššie čísla správne výsledky.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@usamec: Ok beriem, ale stale to este neni uplne ono. http://nayuki.eigenstate.org/page/fast-fibonacci-algorithms
O com mi vsak slo naozaj. Microbenchmarky a porovnavanie rychlosti jazykov su podla mna zlo, lebo ten cas co usetris na rychlosti kodu, nehra vo vacsine aplikacii ziadnu vyznamnu rolu. Lepsim algoritmom ziskas mnohonasobne viac.