add benchmark for Object

This commit is contained in:
Watson
2014-01-27 10:24:23 +09:00
parent e83e74eba9
commit 9a22ec626a
9 changed files with 105 additions and 0 deletions

View File

@@ -37,4 +37,7 @@ autorelease_pool { bm_rational }
puts "*** Module ***"
autorelease_pool { bm_module }
puts "*** Object ***"
autorelease_pool { bm_object }
exit(0)

View File

@@ -0,0 +1,18 @@
class User
def initialize
@name = "foo"
@age = 20
end
end
def bm_object
Benchmark.benchmark("", 30, "%r\n") do |x|
object_instance_eval(x)
object_instance_of(x)
object_instance_variable_get(x)
object_instance_variable_set(x)
object_is_a(x)
object_respond_to(x)
object_send(x)
end
end

View File

@@ -0,0 +1,8 @@
def object_instance_eval(x)
obj = Object.new
x.report "instance_eval" do
2000000.times do
obj.instance_eval { }
end
end
end

View File

@@ -0,0 +1,21 @@
def object_instance_of(x)
obj = Object.new
x.report "instance_of Object" do
2000000.times do
obj.instance_of?(Object)
end
end
x.report "instance_of Fixnum" do
2000000.times do
1.instance_of?(Fixnum)
end
end
string = "foo"
x.report "instance_of String" do
2000000.times do
string.instance_of?(String)
end
end
end

View File

@@ -0,0 +1,9 @@
def object_instance_variable_get(x)
user = User.new
x.report "instance_variable_get" do
1000000.times do
user.instance_variable_get(:@name)
end
end
end

View File

@@ -0,0 +1,9 @@
def object_instance_variable_set(x)
user = User.new
x.report "instance_variable_set" do
1000000.times do
user.instance_variable_set(:@age, 42)
end
end
end

View File

@@ -0,0 +1,21 @@
def object_is_a(x)
obj = Object.new
x.report "is_a? Object" do
2000000.times do
obj.is_a?(Object)
end
end
x.report "is_a? Fixnum" do
2000000.times do
1.is_a?(Fixnum)
end
end
string = "foo"
x.report "is_a? String" do
2000000.times do
string.is_a?(String)
end
end
end

View File

@@ -0,0 +1,8 @@
def object_respond_to(x)
obj = Object.new
x.report "respond_to" do
2000000.times do
obj.respond_to?(:nil?)
end
end
end

View File

@@ -0,0 +1,8 @@
def object_send(x)
obj = Object.new
x.report "send" do
2000000.times do
obj.send(:nil?)
end
end
end