Created
April 18, 2011 12:56
-
-
Save inkel/925274 to your computer and use it in GitHub Desktop.
fib(30) in Ruby & PHP
This file contains 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
<?php | |
function fib($n) { | |
return ($n > 1) ? fib($n - 2) + fib($n - 1) : $n; | |
} | |
echo fib(30); |
This file contains 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
def fib n | |
if n > 1 | |
fib(n - 2) + fib(n - 1) | |
else | |
n | |
end | |
end | |
puts fib(30) |
This file contains 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
$ time php fibo.php | |
832040 | |
real 0m1.342s | |
user 0m1.320s | |
sys 0m0.000s | |
$ time ruby fibo.rb | |
832040 | |
real 0m0.330s | |
user 0m0.320s | |
sys 0m0.010s |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Stupid test I did at work to show a fellow programmer that Ruby is not as slow as it used to be