Skip to content

Instantly share code, notes, and snippets.

@adacosta
Created May 25, 2011 21:03
Show Gist options
  • Save adacosta/991972 to your computer and use it in GitHub Desktop.
Save adacosta/991972 to your computer and use it in GitHub Desktop.
ruby string compare
require 'benchmark'
def repeat(n=10_000_000, &blk); n.times {yield}; end
string = 'string'
Benchmark.bm do |x|
x.report("warm-up single quotes") { repeat {'this is a string'} }
x.report("interpolated double quotes with variable") { repeat {"this is a #{string}"} }
x.report("double quotes") { repeat {"this is a string"} }
x.report("single quotes") { repeat {'this is a string'} }
x.report("interpolated brackets") { repeat {%Q[this is a string]} }
x.report("hex") {repeat {[0x74, 0x68, 0x69, 0x73, 0x20, 0x69, 0x73, 0x20, 0x61, 0x20, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67]} }
end
# user system total real
# warm-up single quotes 1.410000 0.000000 1.410000 ( 1.410171)
# interpolated double quotes with variable 2.380000 0.000000 2.380000 ( 2.384271)
# double quotes 1.400000 0.010000 1.410000 ( 1.398493)
# single quotes 1.400000 0.000000 1.400000 ( 1.398184)
# interpolated brackets 1.390000 0.000000 1.390000 ( 1.389686)
# hex 2.670000 0.130000 2.800000 ( 2.791863)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment