Skip to content

Instantly share code, notes, and snippets.

@jkraemer
Last active July 5, 2016 11:26
Show Gist options
  • Save jkraemer/8f7c0ac282e504e467aff389d6ce145c to your computer and use it in GitHub Desktop.
Save jkraemer/8f7c0ac282e504e467aff389d6ce145c to your computer and use it in GitHub Desktop.
logging and string interpolation
require 'benchmark'
require 'logger'
iterations = 1000000
logger = Logger.new(STDOUT)
logger.level = Logger::ERROR
bar = 'bar'
foo = 'foo'
def method
'method'
end
Benchmark.bmbm(20) do |bm|
bm.report('{ "foo #{bar}" }') do
iterations.times do
logger.debug { "foo #{bar}" }
end
end
bm.report('"#{method} bar"') do
iterations.times do
logger.debug "#{method} bar"
end
end
bm.report('"#{foo} #{bar}"') do
iterations.times do
logger.debug "#{foo} #{bar}"
end
end
bm.report('"foo #{bar}"') do
iterations.times do
logger.debug "foo #{bar}"
end
end
bm.report('"foo bar"') do
iterations.times do
logger.debug "foo bar"
end
end
end
user system total real
{ "foo #{bar}" } 0.430000 0.000000 0.430000 ( 0.423957)
"#{method} bar" 0.400000 0.000000 0.400000 ( 0.399179)
"#{foo} #{bar}" 0.380000 0.000000 0.380000 ( 0.377865)
"foo #{bar}" 0.300000 0.000000 0.300000 ( 0.304749)
"foo bar" 0.220000 0.000000 0.220000 ( 0.214499)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment