Skip to content

Instantly share code, notes, and snippets.

@284km
Last active January 22, 2018 09:33
Show Gist options
  • Save 284km/b93e01662b0c9d2a3916fcff8105f4a5 to your computer and use it in GitHub Desktop.
Save 284km/b93e01662b0c9d2a3916fcff8105f4a5 to your computer and use it in GitHub Desktop.
single_quote-vs-double_quote.rb
require 'benchmark/ips'
def fast
''
end
def slow
""
end
def fast_with_message
'hello world!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!'
end
def slow_with_message
"hello world!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
end
Benchmark.ips do |x|
x.report('single_quote') { fast }
x.report('double_quote') { slow }
x.report('single_quote_with_message') { fast_with_message }
x.report('double_quote_with_message') { slow_with_message }
x.compare!
end
__END__
$ ruby single_quote-vs-double_quote.rb
Warming up --------------------------------------
single_quote 227.716k i/100ms
double_quote 185.211k i/100ms
single_quote_with_message
180.561k i/100ms
double_quote_with_message
210.071k i/100ms
Calculating -------------------------------------
single_quote 6.556M (±14.4%) i/s - 32.108M in 5.001760s
double_quote 6.396M (±13.2%) i/s - 31.486M in 5.013764s
single_quote_with_message
6.167M (±15.1%) i/s - 30.154M in 5.023311s
double_quote_with_message
5.843M (±19.9%) i/s - 27.939M in 5.004880s
Comparison:
single_quote: 6556214.2 i/s
double_quote: 6395775.8 i/s - same-ish: difference falls within error
single_quote_with_message: 6166603.6 i/s - same-ish: difference falls within error
double_quote_with_message: 5843498.3 i/s - same-ish: difference falls within error
ruby single_quote-vs-double_quote.rb 27.48s user 0.19s system 97% cpu 28.513 total
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment