Created
December 19, 2010 00:23
-
-
Save sferik/747003 to your computer and use it in GitHub Desktop.
String Benchmarks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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