Created
May 13, 2013 14:49
-
-
Save jits/5568857 to your computer and use it in GitHub Desktop.
This file contains 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 "benchmark" | |
puts RUBY_DESCRIPTION | |
TIMES = 1_000_000 | |
X_STRING = "x" | |
X_SYMBOL = :x | |
Benchmark.bm(7) do |x| | |
a_string = "x" | |
b_string = "x" | |
a_symbol = :x | |
b_symbol = :x | |
# Strings | |
x.report("String: variable == variable") do | |
1.upto(TIMES) { a_string == b_string } | |
end | |
x.report("String: variable == string") do | |
1.upto(TIMES) { a_string == "x" } | |
end | |
x.report("String: variable == constant") do | |
1.upto(TIMES) { a_string == X_STRING } | |
end | |
# Symbols | |
x.report("Symbol: variable == variable") do | |
1.upto(TIMES) { a_symbol == b_symbol } | |
end | |
x.report("Symbol: variable == string") do | |
1.upto(TIMES) { a_symbol == :x } | |
end | |
x.report("Symbol: variable == constant") do | |
1.upto(TIMES) { a_symbol == X_SYMBOL } | |
end | |
end |
This file contains 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
> ruby benchmark_string_symbol_comparisons.rb | |
ruby 1.9.3p327 (2012-11-10 revision 37606) [x86_64-darwin11.4.2] | |
user system total real | |
String: variable == variable 0.120000 0.000000 0.120000 ( 0.116354) | |
String: variable == string 0.210000 0.000000 0.210000 ( 0.217801) | |
String: variable == constant 0.110000 0.000000 0.110000 ( 0.104247) | |
Symbol: variable == variable 0.160000 0.000000 0.160000 ( 0.176373) | |
Symbol: variable == string 0.140000 0.000000 0.140000 ( 0.135777) | |
Symbol: variable == constant 0.150000 0.000000 0.150000 ( 0.154333) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment