Module Include Stress Test

T

Trans

Was curious about how fast method lookup was in Ruby under various
circumstances. So I wrote this samll stress test. It included moudle
within module up $n deep and times a method call to the middle of it
all. Interestingly it words fine at 1000 levels deep, takingless then a
second to generate the modules, but make it 10000 and instead of just
taking 10 times longer, my system comes to it's knees. I assume it has
to do with memory use. I have 512 MB of RAM, do modules/classes really
eat that much memory?

$n = 1000 # try 10000

puts "Generating #{$n} cascading modules..."

$m = []
(1..$n).each { |i|
$m = Module.new
$m.class_eval {
define_method( "_#{i}" ) { puts "Called method in module #{i}." }
include $m[i-1] if $m[i-1]
}
}

class ManyIncludes
include $m[$n]
end

mm = ManyIncludes.new
mn = "_#{$n/2}"

t0 = Time.now
mm.send(mn)
t1 = Time.now
puts "Time to call a single method among #{$n} modules: #{(t1 -
t0).to_f} seconds."

T.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,176
Messages
2,570,950
Members
47,500
Latest member
ArianneJsb

Latest Threads

Top