Skip to content

Instantly share code, notes, and snippets.

@hasclass
Created October 27, 2010 15:28
Show Gist options
  • Save hasclass/649256 to your computer and use it in GitHub Desktop.
Save hasclass/649256 to your computer and use it in GitHub Desktop.
require 'benchmark'
class Foo
def method_missing(method_name, *args)
if method_name.match(/foo_bar/)
# code
else
super(method_name, *args)
end
end
end
GC.disable # Don't let GarbageCollector change outcome
Benchmark.bm(40) do |b|
n = 100_000
normal_objects = n.times.map{Object.new}
foo_objects = n.times.map{Foo.new}
b.report("flatten Object objects") { normal_objects.flatten }
b.report("flatten Foo objects") { foo_objects.flatten }
end
GC.enable
# Results
# user system total real
# flatten Object objects 0.000000 0.000000 0.000000 ( 0.013034)
# flatten Foo objects 3.740000 1.560000 5.300000 ( 11.905745)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment