Last active
August 29, 2015 14:06
-
-
Save evotopid/38cac27cb8b8a406f7c1 to your computer and use it in GitHub Desktop.
Ruby Benchmark: dynamic anonymous class definition vs. named class
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
user system total real | |
named class 0.250000 0.010000 0.260000 ( 0.247367) | |
anonymous class 5.230000 0.170000 5.400000 ( 5.403844) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Not a surprising result, but this shows that anyone using lots of anonymous classes should consider if possible to use named/defined ones.