1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
|
require 'benchmark/ips'
Custom = Class.new(Object)
# create these once and hold references
SINGLETON_COUNT = 1000
many_singletons = SINGLETON_COUNT.times.map { c = Custom.new; class << c; end; c }
Benchmark.ips do |bm|
[1, 5, 10, 50].each do |count|
bm.report("#{count} thread Numeric.subclasses") do
count.times.map {
Thread.new {
i = 10_000 / count
while i > 0
Numeric.subclasses
i-=1
end
}
}.each(&:join)
end
bm.report("#{count} thread Object.subclasses") do
count.times.map {
Thread.new {
i = 10_000 / count
while i > 0
Object.subclasses
i-=1
end
}
}.each(&:join)
end
bm.report("#{count} thread Custom.subclasses with #{SINGLETON_COUNT} singletons") do
count.times.map {
Thread.new {
i = 10_000 / count
while i > 0
Custom.subclasses
i-=1
end
}
}.each(&:join)
end
end
end
|