Created
February 5, 2009 10:25
-
-
Save hipertracker/58642 to your computer and use it in GitHub Desktop.
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
# http://confluence.concord.org/display/CCTR/Comparison+of+Ruby+1.8.6+1.9+and+JRuby+running+on+Java+1.5+1.6+and+1.7 | |
#!/usr/bin/env ruby | |
# | |
# Ruby 1.8.6, 1.9, and JRuby running on Java 1.5, 1.6, and 1.7 compared | |
# | |
# Stephen Bannasch, Feb 4, 2009 | |
# | |
# mergesort benchmark adapted from: | |
# | |
# http://izumi.plan99.net/blog/index.php/2008/01/17/ruby-vs-php-performance/ | |
# | |
# The measurements below are from running the mergesort test | |
# twice in sucession: | |
# | |
# Ruby version time speed * | |
# ------------------------------------------------ | |
# 1.8.6 patchlevel 114 9.33 0.42 | |
# 1.9 svn rev: 20339 3.90 1.0 | |
# | |
# Java 1.5 | |
# jruby, 3.96 0.99 | |
# jruby --server, 3.00 1.30 | |
# jruby --fast, 3.34 1.17 * | |
# jruby --fast --server, 2.42 1.61 * | |
# | |
# Java 1.6 | |
# jruby, 1.85 2.10 | |
# jruby --server, 1.90 2.06 | |
# jruby --fast, 1.57 2.47 * | |
# jruby --fast --server, 1.67 2.34 * | |
# | |
# Java 1.7 | |
# jruby, 2.26 1.73 | |
# jruby --server, 2.06 1.90 | |
# jruby --fast, 2.04 1.91 * | |
# jruby --fast --server, 1.75 2.23 * | |
# | |
# * speed normalized to Ruby 1.9, higher is better | |
# | |
# * running jruby with the '--fast' option is experimental | |
# see: http://wiki.jruby.org/wiki/Performance_Tuning | |
# | |
# computer: | |
# MacBook Pro, 2.5GHz, 4GB memory, Mac OS X 10.5.6 | |
# | |
# Ruby 1.8.6 | |
# ruby 1.8.6 (2008-03-03 patchlevel 114) [universal-darwin9.0] | |
# | |
# Ruby 1.9 | |
# trunk: svn rev:20339 (Feb 2 2009) | |
# | |
# JRuby: | |
# jruby 1.2.0 (ruby 1.8.6 patchlevel 287) (2009-02-04 rev 6586) [i386-java] | |
# | |
# Java 1.5: | |
# java version "1.5.0_16" | |
# Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0_16-b06-284) | |
# Java HotSpot(TM) Client VM (build 1.5.0_16-133, mixed mode, sharing) | |
# | |
# Java 1.6 | |
# java version "1.6.0_07" | |
# Java(TM) SE Runtime Environment (build 1.6.0_07-b06-153) | |
# Java HotSpot(TM) 64-Bit Server VM (build 1.6.0_07-b06-57, mixed mode) | |
# | |
# Java 1.7 | |
# openjdk version "1.7.0-internal" | |
# OpenJDK Runtime Environment (build 1.7.0-internal-stephen_2009_01_25_23_54-b00) | |
# OpenJDK Server VM (build 14.0-b10, mixed mode) | |
require 'rubygems' | |
require 'benchmark' | |
if RUBY_PLATFORM =~ /java/ | |
@ruby_info = "Ruby version: JRuby #{RUBY_VERSION} (#{RUBY_RELEASE_DATE} rev #{RUBY_PATCHLEVEL}) [i386-jruby#{JRUBY_VERSION}]" | |
@ruby_info << ", platform: Java, version #{java.lang.System.getProperty('java.version')}" | |
else | |
@ruby_info = "Ruby version: MRI #{RUBY_VERSION} (#{RUBY_RELEASE_DATE} rev #{RUBY_PATCHLEVEL})" | |
@ruby_info << ", platform: #{RUBY_PLATFORM}" | |
end | |
def merge(a, b) | |
c = [] | |
while !a.empty? && !b.empty? | |
if a[0] > b[0] | |
c << b[0] | |
b.shift | |
else | |
c << a[0] | |
a.shift | |
end | |
end | |
while !a.empty? | |
c << a[0] | |
a.shift | |
end | |
while !b.empty? | |
c << b[0] | |
b.shift | |
end | |
return c | |
end | |
def mergesort(a) | |
n = a.size | |
if n <= 1 | |
return a | |
end | |
l1 = [] | |
l2 = [] | |
i = 0 | |
while i < n / 2 | |
l1 << a[i] | |
i += 1 | |
end | |
while i < n | |
l2 << a[i] | |
i += 1 | |
end | |
l1 = mergesort(l1) | |
l2 = mergesort(l2) | |
return merge(l1, l2) | |
end | |
def run_mergesort | |
numbers = [47448054, 1106251565, 1208921855, 170086026, 840395770, 444281018, 1297307905, 1613614128, 357068250, 1829657695, 654555439, 1261773796, 1821640729, 449683981, 1062536538, 96076061, 1387478498, 1835855315, 364455615, 4830124, 864633601, 289493189, 471351435, 435996916, 1366312031, 888420407, 1923379522, 735726044, 1094401518, 245520239, 109946712, 1107893495, 592868510, 700148765, 273016388, 343881444, 420725947, 1259049694, 1692920986, 71271532, 1154617350, 593508009, 1106700528, 430204045, 1045928775, 1330476642, 49983990, 1451164767, 1175404600, 644832496, 365016297, 1048732794, 503615317, 217186301, 1176160338, 1183622513, 81711049, 1720671278, 1393072097, 1315236388, 1451774341, 92848458, 271000544, 1667871288, 380233084, 1053079658, 1249341507, 1276652307, 1722015039, 1243698025, 178813868, 1449271074, 1994327579, 270972819, 1043379189, 1592595484, 462468972, 1464773315, 1994172406, 997300623, 46405283, 1614271949, 447907123, 317292284, 378291676, 1253835093, 523476912, 1606023999, 59263848, 1234358080, 140981643, 1828471854, 1197394207, 1317927546, 878287915, 334576359, 982149842, 642878238, 1024064999, 1834342299]; | |
3000.times do | |
mergesort(numbers) | |
end | |
end | |
n = 2 | |
times = "time" + (n>1 ? 's' : '') | |
puts | |
puts @ruby_info | |
puts | |
puts "Running Hongli Lai's Ruby mergesort benchmark #{n} #{times}" | |
puts | |
puts "See: http://izumi.plan99.net/blog/index.php/2008/01/17/ruby-vs-php-performance/" | |
puts | |
2.times do | |
Benchmark.bmbm do |x| | |
x.report("mergesort") { n.times {run_mergesort} } | |
end | |
puts | |
end | |
# Results: | |
# PowerMac 2x2.8 Xeon | |
# JRuby 1.2 edge) Ruby version: JRuby 1.8.6 (2009-02-05 rev 287) [i386-jruby1.2.0], platform: Java, version 1.6.0_07 | |
# -- fast 1.48 s. | |
# 1.63 s. | |
# ruby 1.8.6 (2008-08-08 patchlevel 286) [i686-darwin9.6.0] | |
# 3.50 s. | |
# Ruby version: REE 1.8.6 (2008-08-08 rev 286), platform: i686-darwin9.6.0 | |
# 7.81 s | |
# Ruby version: MRI 1.8.7 (2008-08-11 rev 72), platform: i686-darwin9 | |
# 7.96 s. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment