mirror of
https://github.com/zhigang1992/RubyMotion.git
synced 2026-04-25 13:25:39 +08:00
add benchmark for Object
This commit is contained in:
@@ -37,4 +37,7 @@ autorelease_pool { bm_rational }
|
||||
puts "*** Module ***"
|
||||
autorelease_pool { bm_module }
|
||||
|
||||
puts "*** Object ***"
|
||||
autorelease_pool { bm_object }
|
||||
|
||||
exit(0)
|
||||
|
||||
18
test/Benchmark/app/benchmark/object.rb
Normal file
18
test/Benchmark/app/benchmark/object.rb
Normal 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
|
||||
8
test/Benchmark/app/benchmark/object/instance_eval.rb
Normal file
8
test/Benchmark/app/benchmark/object/instance_eval.rb
Normal 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
|
||||
21
test/Benchmark/app/benchmark/object/instance_of.rb
Normal file
21
test/Benchmark/app/benchmark/object/instance_of.rb
Normal 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
|
||||
@@ -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
|
||||
@@ -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
|
||||
21
test/Benchmark/app/benchmark/object/is_a.rb
Normal file
21
test/Benchmark/app/benchmark/object/is_a.rb
Normal 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
|
||||
8
test/Benchmark/app/benchmark/object/respond_to.rb
Normal file
8
test/Benchmark/app/benchmark/object/respond_to.rb
Normal 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
|
||||
8
test/Benchmark/app/benchmark/object/send.rb
Normal file
8
test/Benchmark/app/benchmark/object/send.rb
Normal 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
|
||||
Reference in New Issue
Block a user