Skip to content

Instantly share code, notes, and snippets.

@fdc263
Created May 2, 2016 16:35
Show Gist options
  • Save fdc263/16a8e60c749d2677cfd41088fde16e0b to your computer and use it in GitHub Desktop.
Save fdc263/16a8e60c749d2677cfd41088fde16e0b to your computer and use it in GitHub Desktop.
Ruby multi-line string benchmark
require 'benchmark'
iterations = 100_000
store = {'name' => 'Store name', 'host' => 'www.store-url.com'}
Benchmark.bm do |bm|
bm.report do
iterations.times do
"".tap do |code|
code << "<script type=\"application/ld+json\">"
code << "{ \"@context\" : \"http://schema.org\","
code << "\"@type\" : \"WebSite\","
code << "\"name\" : \"#{store['name']}\","
code << "\"alternateName\" : \"#{store['name']}\","
code << "\"url\" : \"http://#{store['host']}/\","
code << "\"potentialAction\": {"
code << "\"@type\": \"SearchAction\","
code << "\"target\": \"http://#{store['host']}/busca?q={q}&referrer=sitelinks_searchbox\","
code << '"query-input": {'
code << '"@type": "PropertyValueSpecification",'
code << '"valueRequired": true,'
code << '"valueMaxlength": 100,'
code << '"valueName": "q"'
code << '}'
code << "}"
code << "}"
code << "</script>"
end
end
end
bm.report do
iterations.times do
code = %{
<script type="application/ld+json">
{
"@context" : "http://schema.org",
"@type" : "WebSite",
"name" : "#{store['name']}",
"alternateName" : "#{store['name']}",
"url" : "http://#{store['host']}/",
"potentialAction": {
"@type": "SearchAction",
"target": "http://#{store['host']}/busca?q={q}&referrer=sitelinks_searchbox",
"query-input": {
"@type": "PropertyValueSpecification",
"valueRequired": true,
"valueMaxlength": 100,
"valueName": "q"
}"
}"
}
</script>"
}
code
end
end
bm.report do
iterations.times do
code = "<script type=\"application/ld+json\">" \
"{ \"@context\" : \"http://schema.org\"," \
"\"@type\" : \"WebSite\"," \
"\"name\" : \"#{store['name']}\"," \
"\"alternateName\" : \"#{store['name']}\"," \
"\"url\" : \"http://#{store['host']}/\"," \
"\"potentialAction\": {" \
"\"@type\": \"SearchAction\"," \
"\"target\": \"http://#{store['host']}/busca?q={q}&referrer=sitelinks_searchbox\"," \
'"query-input": {' \
'"@type": "PropertyValueSpecification",' \
'"valueRequired": true,' \
'"valueMaxlength": 100,' \
'"valueName": "q"' \
'}'\
"}" \
"}" \
"</script>"
code
end
end
end
user system total real
0.710000 0.000000 0.710000 ( 0.714813) #actual implementation
0.250000 0.000000 0.250000 ( 0.242231) #output with whitespaces (701 chars)
0.230000 0.000000 0.230000 ( 0.236310) #output without whitespaces (433 chars)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment