Created
November 16, 2009 06:23
-
-
Save shino/235787 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
class Sandbag1 | |
def self.add1 | |
define_method(:hoge1){ puts "The method :hoge1 is called."; } | |
end | |
def self.remove1 | |
undef_method :hoge1 | |
rescue NameError => e | |
# puts e | |
end | |
def self.add2 | |
define_method(:hoge2){ puts "The method :hoge2 is called."; } | |
end | |
def self.remove2 | |
undef_method :hoge2 | |
rescue NameError => e | |
# puts e | |
end | |
def fib(n) | |
return 0 if n == 0 | |
return 1 if n == 1 | |
fib(n-2) + fib(n-1) | |
end | |
end | |
class Sandbag2 | |
def self.add1 | |
define_method(:hoge1){ puts "The method :hoge1 is called."; } | |
end | |
def self.remove1 | |
undef_method :hoge1 | |
rescue NameError => e | |
# puts e | |
end | |
end | |
fib_n = ARGV[0].to_i | |
add_rem_n = ARGV[1].to_i | |
times_benchmark = 4 | |
require "benchmark" | |
puts | |
puts "Almost no lock contention (calculate Fibonacchi sequence)" | |
Benchmark.bm(10) { |bm| | |
times_benchmark.times { |i| | |
bm.report("1 thread (#{i})") { | |
t1 = Thread.new(fib_n){|n| Sandbag1.new.fib(n) } | |
t1.join | |
} | |
bm.report("2 threads (#{i})") { | |
t1 = Thread.new(fib_n){|n| Sandbag1.new.fib(n) } | |
t2 = Thread.new(fib_n){|n| Sandbag1.new.fib(n) } | |
t1.join | |
t2.join | |
} | |
} | |
} | |
puts | |
puts "Add/remove the SAME method for the SAME class object" | |
Benchmark.bm(10) { |bm| | |
times_benchmark.times { |i| | |
bm.report("1 thread (#{i})") { | |
t1 = Thread.new(add_rem_n){|n| n.times{|m| Sandbag1.add1; Sandbag1.remove1; }} | |
t1.join | |
} | |
bm.report("2 threads (#{i})") { | |
t1 = Thread.new(add_rem_n){|n| n.times{|m| Sandbag1.add1; Sandbag1.remove1; }} | |
t2 = Thread.new(add_rem_n){|n| n.times{|m| Sandbag1.add1; Sandbag1.remove1; }} | |
t1.join | |
t2.join | |
} | |
} | |
} | |
puts | |
puts "Add/remove different methods for the SAME class object" | |
Benchmark.bm(10) { |bm| | |
times_benchmark.times { |i| | |
bm.report("1 thread (#{i})") { | |
t1 = Thread.new(add_rem_n){|n| n.times{|m| Sandbag1.add1; Sandbag1.remove1; }} | |
t1.join | |
} | |
bm.report("2 threads (#{i})") { | |
t1 = Thread.new(add_rem_n){|n| n.times{|m| Sandbag1.add1; Sandbag1.remove1; }} | |
t2 = Thread.new(add_rem_n){|n| n.times{|m| Sandbag1.add2; Sandbag1.remove2; }} | |
t1.join | |
t2.join | |
} | |
} | |
} | |
puts | |
puts "Add/remove different methods for different class objects" | |
Benchmark.bm(10) { |bm| | |
times_benchmark.times { |i| | |
bm.report("1 thread (#{i})") { | |
t1 = Thread.new(add_rem_n){|n| n.times{|m| Sandbag1.add1; Sandbag1.remove1; }} | |
t1.join | |
} | |
bm.report("2 threads (#{i})") { | |
t1 = Thread.new(add_rem_n){|n| n.times{|m| Sandbag1.add1; Sandbag1.remove1; }} | |
t2 = Thread.new(add_rem_n){|n| n.times{|m| Sandbag2.add1; Sandbag2.remove1; }} | |
t1.join | |
t2.join | |
} | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment