add benchmark for Rational

This commit is contained in:
Watson
2014-01-13 02:02:19 +09:00
parent e7fb463e8d
commit 88f581dff1
8 changed files with 105 additions and 1 deletions

View File

@@ -28,4 +28,7 @@ autorelease_pool { bm_string }
puts "*** Time ***"
autorelease_pool { bm_time }
puts "*** Rational ***"
autorelease_pool { bm_rational }
exit(0)

View File

@@ -0,0 +1,9 @@
def bm_rational
Benchmark.benchmark("", 30, "%r\n") do |x|
rational_new(x)
rational_plus(x)
rational_minus(x)
rational_divide(x)
rational_multiply(x)
end
end

View File

@@ -0,0 +1,21 @@
def rational_divide(x)
rat1 = Rational(3, 2.5)
rat2 = Rational(3, 4)
x.report "/ Rational" do
500000.times do
rat1 / rat2
end
end
x.report "/ Fixnum" do
500000.times do
rat1 / 10
end
end
x.report "/ Float" do
500000.times do
rat1 / 4.5
end
end
end

View File

@@ -0,0 +1,21 @@
def rational_minus(x)
rat1 = Rational(3, 2.5)
rat2 = Rational(3, 4)
x.report "- Rational" do
500000.times do
rat1 - rat2
end
end
x.report "- Fixnum" do
500000.times do
rat1 - 10
end
end
x.report "- Float" do
500000.times do
rat1 - 4.5
end
end
end

View File

@@ -0,0 +1,21 @@
def rational_multiply(x)
rat1 = Rational(3, 2.5)
rat2 = Rational(3, 4)
x.report "* Rational" do
500000.times do
rat1 * rat2
end
end
x.report "* Fixnum" do
500000.times do
rat1 * 10
end
end
x.report "* Float" do
500000.times do
rat1 * 4.5
end
end
end

View File

@@ -0,0 +1,8 @@
def rational_new(x)
x.report "Rational(x, y)" do
200000.times do
Rational(3, 2.5)
end
end
end

View File

@@ -0,0 +1,21 @@
def rational_plus(x)
rat1 = Rational(3, 2.5)
rat2 = Rational(3, 4)
x.report "+ Rational" do
500000.times do
rat1 + rat2
end
end
x.report "+ Fixnum" do
500000.times do
rat1 + 10
end
end
x.report "+ Float" do
500000.times do
rat1 + 4.5
end
end
end

2
vm

Submodule vm updated: 91ab4d371a...e146f16b26