mirror of
https://github.com/zhigang1992/RubyMotion.git
synced 2026-05-26 15:43:57 +08:00
add benchmark for Module
This commit is contained in:
@@ -31,4 +31,7 @@ autorelease_pool { bm_time }
|
||||
puts "*** Rational ***"
|
||||
autorelease_pool { bm_rational }
|
||||
|
||||
puts "*** Module ***"
|
||||
autorelease_pool { bm_module }
|
||||
|
||||
exit(0)
|
||||
|
||||
24
test/Benchmark/app/benchmark/module.rb
Normal file
24
test/Benchmark/app/benchmark/module.rb
Normal file
@@ -0,0 +1,24 @@
|
||||
module Baz
|
||||
CONST_BAZ = "baz"
|
||||
@@baz = "baz"
|
||||
end
|
||||
|
||||
class Foo
|
||||
CONST_FOO = 42
|
||||
@@foo = "foo"
|
||||
end
|
||||
|
||||
class Bar
|
||||
include Baz
|
||||
end
|
||||
|
||||
def bm_module
|
||||
Benchmark.benchmark("", 30, "%r\n") do |x|
|
||||
module_alias_method(x)
|
||||
module_class_eval(x)
|
||||
module_class_variable_get(x)
|
||||
module_class_variable_set(x)
|
||||
module_const_get(x)
|
||||
module_const_set(x)
|
||||
end
|
||||
end
|
||||
9
test/Benchmark/app/benchmark/module/alias_method.rb
Normal file
9
test/Benchmark/app/benchmark/module/alias_method.rb
Normal file
@@ -0,0 +1,9 @@
|
||||
def module_alias_method(x)
|
||||
x.report "alias_method" do
|
||||
10000.times do |i|
|
||||
Foo.class_eval {
|
||||
alias_method(:"test#{i}", :to_s)
|
||||
}
|
||||
end
|
||||
end
|
||||
end
|
||||
8
test/Benchmark/app/benchmark/module/class_eval.rb
Normal file
8
test/Benchmark/app/benchmark/module/class_eval.rb
Normal file
@@ -0,0 +1,8 @@
|
||||
def module_class_eval(x)
|
||||
x.report "class_eval" do
|
||||
1000000.times do
|
||||
Object.class_eval {}
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
14
test/Benchmark/app/benchmark/module/class_variable_get.rb
Normal file
14
test/Benchmark/app/benchmark/module/class_variable_get.rb
Normal file
@@ -0,0 +1,14 @@
|
||||
def module_class_variable_get(x)
|
||||
x.report "class_variable_get" do
|
||||
1000000.times do
|
||||
Foo.class_variable_get(:@@foo)
|
||||
end
|
||||
end
|
||||
|
||||
x.report "class_variable_get (included)" do
|
||||
1000000.times do
|
||||
Bar.class_variable_get(:@@baz)
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
14
test/Benchmark/app/benchmark/module/class_variable_set.rb
Normal file
14
test/Benchmark/app/benchmark/module/class_variable_set.rb
Normal file
@@ -0,0 +1,14 @@
|
||||
def module_class_variable_set(x)
|
||||
x.report "class_variable_set" do
|
||||
100000.times do |i|
|
||||
Foo.class_variable_set(:"@@foo#{i}", 42)
|
||||
end
|
||||
end
|
||||
|
||||
x.report "class_variable_set (included)" do
|
||||
100000.times do |i|
|
||||
Bar.class_variable_set(:"@@baz#{i}", 42)
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
14
test/Benchmark/app/benchmark/module/const_get.rb
Normal file
14
test/Benchmark/app/benchmark/module/const_get.rb
Normal file
@@ -0,0 +1,14 @@
|
||||
def module_const_get(x)
|
||||
x.report "const_get" do
|
||||
1000000.times do
|
||||
Foo.const_get(:CONST_FOO)
|
||||
end
|
||||
end
|
||||
|
||||
x.report "const_get (included)" do
|
||||
1000000.times do
|
||||
Bar.const_get(:CONST_BAZ)
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
14
test/Benchmark/app/benchmark/module/const_set.rb
Normal file
14
test/Benchmark/app/benchmark/module/const_set.rb
Normal file
@@ -0,0 +1,14 @@
|
||||
def module_const_set(x)
|
||||
x.report "const_set" do
|
||||
100000.times do |i|
|
||||
Foo.const_set(:"CONST_FOO#{i}", 42)
|
||||
end
|
||||
end
|
||||
|
||||
x.report "const_set (included)" do
|
||||
100000.times do |i|
|
||||
Bar.const_set(:"CONST_BAZ#{i}", 42)
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
Reference in New Issue
Block a user