Last active
September 25, 2015 22:38
-
-
Save olauzon/995885 to your computer and use it in GitHub Desktop.
Benchmarks
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
# Array#join vs String#+ | |
require 'benchmark' | |
n = 1_000_000 | |
Benchmark.bm do |x| | |
x.report('Array#join :') { | |
n.times { ['this', 'is', 'a', 'test'].join(' ') } | |
} | |
x.report('String#+ :') { | |
n.times { 'this' + ' ' + 'is' + ' ' + 'a' + ' ' + 'test' } | |
} | |
end | |
# RUBY_VERSION = '1.8.7' | |
# user system total real | |
# Array#join : 3.120000 0.020000 3.140000 (3.568955) | |
# String#+ : 5.400000 0.050000 5.450000 (6.579634) | |
# RUBY_VERSION = 'ruby-1.9.3-p194' | |
# user system total real | |
#Array#join : 2.980000 0.020000 3.000000 (3.075411) | |
#String#+ : 3.290000 0.010000 3.300000 (3.346815) | |
# ruby -v | |
# ruby 2.1.1p76 (2014-02-24 revision 45161) [x86_64-darwin12.0] | |
# user system total real | |
# Array#join : 1.270000 0.000000 1.270000 ( 1.269124) | |
# String#+ : 0.880000 0.000000 0.880000 ( 0.886282) |
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' | |
n = 10_000_000 | |
val1 = 'test' | |
Benchmark.bm do |x| | |
x.report('String#{} :') { | |
n.times { "this is a #{val1}" } | |
} | |
x.report('Array#join :') { | |
n.times { ['this is a', val1].join(' ') } | |
} | |
x.report('String#+ :') { | |
n.times { 'this is a ' + val1 } | |
} | |
end | |
# ruby -v | |
# ruby 2.1.1p76 (2014-02-24 revision 45161) [x86_64-darwin12.0] | |
# user system total real | |
# String#{} : 1.700000 0.000000 1.700000 ( 1.703822) | |
# Array#join : 6.620000 0.000000 6.620000 ( 6.622582) | |
# String#+ : 1.770000 0.010000 1.780000 ( 1.781350) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
+1