Skip to content

Instantly share code, notes, and snippets.

@sferik
Created December 19, 2010 00:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sferik/747003 to your computer and use it in GitHub Desktop.
Save sferik/747003 to your computer and use it in GitHub Desktop.
String Benchmarks
require "rubygems"
require "rbench"
def single_quoted_string
'aaa' + 'bbb'
end
def double_quoted_string
"aaa" + "bbb"
end
def percent_string
%(aaa) + %(bbb)
end
def percent_q_string
%q(aaa) + %q(bbb)
end
RBench.run(1_000_000) do
column :one, :title => "Single-Quoted String"
column :two, :title => "Double-Quoted String"
column :three, :title => "Percent String"
column :four, :title => "Percent-Q String"
report "String Speed" do
one { single_quoted_string }
two { double_quoted_string }
three { percent_string }
four { percent_q_string }
end
end
__END__
Single-Quoted String | Double-Quoted String | Percent String | Percent-Q String |
------------------------------------------------------------------------------------------------------
String Speed 0.558 | 0.587 | 0.576 | 0.543 |
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment