Skip to content

Instantly share code, notes, and snippets.

@evotopid
Last active August 29, 2015 14:06
Show Gist options
  • Save evotopid/38cac27cb8b8a406f7c1 to your computer and use it in GitHub Desktop.
Save evotopid/38cac27cb8b8a406f7c1 to your computer and use it in GitHub Desktop.
Ruby Benchmark: dynamic anonymous class definition vs. named class
require 'benchmark'
N = 500_000
Benchmark.bm(15) do |x|
x.report("named class") do
class A
def initialize
@a = 1234
end
def run
@a
end
end
N.times{ A.new.run }
end
x.report("anonymous class") do
N.times{
Class.new{
def initialize
@a = 1234
end
def run
@a
end
}.new.run
}
end
end
user system total real
named class 0.250000 0.010000 0.260000 ( 0.247367)
anonymous class 5.230000 0.170000 5.400000 ( 5.403844)
@evotopid
Copy link
Author

evotopid commented Sep 9, 2014

Not a surprising result, but this shows that anyone using lots of anonymous classes should consider if possible to use named/defined ones.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment