Created
April 3, 2014 17:07
-
-
Save binarycleric/9958595 to your computer and use it in GitHub Desktop.
Using a single object vs. creating a new one each time. Pretty basic Ruby stuff but I still see this anti-pattern all over the place.
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
class ClassThatDoesStuff | |
def some_method | |
"You're winner!" | |
end | |
end | |
require 'benchmark' | |
puts "Using a single instance : 5000 times" | |
Benchmark.benchmark do |x| | |
x.report do | |
doer = ClassThatDoesStuff.new | |
5000.times{|n| doer.some_method} | |
end | |
end | |
puts "\n" | |
puts "Creating an instance each time : 5000 times" | |
Benchmark.benchmark do |x| | |
x.report do | |
5000.times{|n| ClassThatDoesStuff.new.some_method} | |
end | |
end | |
puts "\n\n" |
Author
binarycleric
commented
Apr 3, 2014
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment