Create a gist now

Instantly share code, notes, and snippets.

Embed
What would you like to do?
require 'benchmark'
require 'logger'
require 'date'
logger = Logger.new("/dev/null")
logger.level = Logger::INFO
array = []
1.upto(10) {|i| array << i}
attributes = {
:id => 1,
:title => "Super Simple Ruby Performance Tip",
:published_at => Date.today,
:tags => ["ruby", "performance", "tips"],
:categories => ["Ruby"]
}
Benchmark.bmbm do |x|
x.report("string form (array.first)") {
100_000.times do
logger.debug "a message with #{array.first}"
end
}
x.report("string form (array.inspect)") {
100_000.times do
logger.debug "a message with #{array.inspect}"
end
}
x.report("string form (attributes.inspect)") {
100_000.times do
logger.debug "a message with #{attributes.inspect}"
end
}
x.report("block form (array.first)") {
100_000.times do
logger.debug { "a message with #{array.first}" }
end
}
x.report("block form (array.inspect)") {
100_000.times do
logger.debug { "a message with #{array.inspect}" }
end
}
x.report("block form (attributes.inspect)") {
100_000.times do
logger.debug { "a message with #{attributes.inspect}" }
end
}
end
puts
# Rehearsal --------------------------------------------------------------------
# string form (array.first)          0.230000   0.000000   0.230000 (  0.239445)
# string form (array.inspect)        0.900000   0.010000   0.910000 (  0.918498)
# string form (attributes.inspect)   2.050000   0.000000   2.050000 (  2.056928)
# block form (array.first)           0.320000   0.010000   0.330000 (  0.330422)
# block form (array.inspect)         0.350000   0.000000   0.350000 (  0.347880)
# block form (attributes.inspect)    0.320000   0.000000   0.320000 (  0.326636)
# ----------------------------------------------------------- total: 4.190000sec
#
#                                        user     system      total        real
# string form (array.first)          0.240000   0.000000   0.240000 (  0.237537)
# string form (array.inspect)        0.900000   0.010000   0.910000 (  0.907226)
# string form (attributes.inspect)   2.040000   0.000000   2.040000 (  2.063976)
# block form (array.first)           0.320000   0.000000   0.320000 (  0.332216)
# block form (array.inspect)         0.330000   0.010000   0.340000 (  0.330169)
# block form (attributes.inspect)    0.330000   0.000000   0.330000 (  0.328436)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment