Skip to content

Instantly share code, notes, and snippets.

@sidonath
Last active March 9, 2016 17:09
Show Gist options
  • Save sidonath/f0e9026818f76373b805 to your computer and use it in GitHub Desktop.
Save sidonath/f0e9026818f76373b805 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
require 'benchmark'
def add_methods_to_a_module(m)
1_000.times.map do |i|
method_name = "method_#{i}"
m.__send__(:define_method, method_name) {}
method_name
end
end
def include_into_other_modules(m, prefix)
10.times do |i|
m = Module.new do
include m
end
# We assign the module to a const to make sure it's not garbage collected
Object.const_set("Module_#{prefix}_#{i}", m)
end
end
module_under_test = Module.new
10.times do |i|
methods = add_methods_to_a_module(module_under_test)
include_into_other_modules(module_under_test, i)
time_to_remove_methods = Benchmark.realtime do
methods.each do |m|
module_under_test.__send__ :undef_method, m
end
end
printf "Time to remove methods: %.2f\n", time_to_remove_methods
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment