Skip to content

Instantly share code, notes, and snippets.

@isokcevic
Created September 24, 2013 08:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save isokcevic/6681913 to your computer and use it in GitHub Desktop.
Save isokcevic/6681913 to your computer and use it in GitHub Desktop.
require 'benchmark'
n = 1000000
Benchmark.bm do |x|
x.report("'Old' string concat:"){ n.times{ "Hello, " + "world" } }
class String
alias_method :old_plus, :+
def +(other)
old_plus(other.to_s)
end
end
class Symbol
def +(other)
self.to_s + other
end
end
x.report("'New' string concat:"){ n.times{"Hello, " + "world"} }
x.report("String + symbol: "){ n.times{"Hello, " + :"world"} }
x.report("Symbol + String: "){ n.times{"Hello, " + :"world"} }
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment