Skip to content

Instantly share code, notes, and snippets.

@lemanchester
Created February 24, 2014 14:59
Show Gist options
  • Save lemanchester/9189848 to your computer and use it in GitHub Desktop.
Save lemanchester/9189848 to your computer and use it in GitHub Desktop.
String Interpolation using Hash Vs. String Interpolation. Benchmark on ruby 1.9.3 to verify which one has a better performance.
user system total real
hash interpolation 0.300000 0.010000 0.310000 ( 0.301211)
string interpolation 0.090000 0.000000 0.090000 ( 0.098942)
user system total real
hash interpolation 0.290000 0.010000 0.300000 ( 0.301891)
string interpolation 0.140000 0.000000 0.140000 ( 0.135461)
user system total real
hash interpolation 0.310000 0.010000 0.320000 ( 0.318774)
string interpolation 0.100000 0.000000 0.100000 ( 0.109029)
user system total real
hash interpolation 0.300000 0.010000 0.310000 ( 0.303394)
string interpolation 0.100000 0.000000 0.100000 ( 0.099212)
require 'benchmark'
module Visit
PLANNED = 'planned'
DATE = '2016-02-13'
end
n = 100000
Benchmark.bm do |x|
x.report('hash interpolation') do
n.times do
s = "visit.status = %{visit_status} AND visit.planned_date < %{visit_planned_date}" % { visit_status: Visit::PLANNED, visit_planned_date: Visit::DATE }
end
end
x.report('string interpolation') do
n.times do
s = "visits.status = '#{Visit::PLANNED}' and visits.planned_date < '#{Visit::DATE}'"
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment