Created
June 5, 2014 00:56
-
-
Save jgaskins/390581d07ead314d75b5 to your computer and use it in GitHub Desktop.
Fibonacci Benchmark: Swift vs Rubinius vs JRuby vs MRI
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
require 'benchmark' | |
def fib n | |
if n <= 1 | |
n | |
else | |
fib(n - 1) + fib(n - 2) | |
end | |
end | |
irb> Benchmark.realtime { fib(40) } | |
# Rubinius: 3.87664 | |
# JRuby : 6.228 | |
# MRI 2.0 : 16.847601 | |
# MRI 2.1 : 14.56271 |
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
import Cocoa | |
func fib(n: Int) -> Int { | |
if n <= 1 { | |
return n | |
} else { | |
return fib(n - 1) + fib(n - 2) | |
} | |
} | |
func benchmark(closure: () -> Int) -> NSTimeInterval { | |
let start = NSDate() | |
closure() | |
return NSDate().timeIntervalSinceDate(start) | |
} | |
27> benchmark { fib(40) } | |
// $R41: NSTimeInterval = { | |
// value = 0.5169379711151123 | |
// } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
JRuby on an invokedynamic-capable JVM should be at least as fast as Rubinius. Here's my numbers.
MRI 2.1.1: 24.554626
JRuby 1.7.12 (Java 8 + invokedynamic): 3.463000
JRuby 1.7.12 (Java 8 - invokedynamic): 6.256000
Rubinius 2.2.6ish: 6.018709
Given your tweet that Swift is 29x faster than MRI, which came in at 24s on my system, I guess that would mean JRuby's only in the <5x slower range. Not bad competing against a statically-compiled and statically-analyzed language.
Of course, JRuby 9000 will have two new runtimes that should easily match or exceed native math performance. JRuby's still your best bet for fast, compatible Ruby.