Skip to content

Commit cec53a2

Browse files
committed
Merge pull request #108 from lvl0nax/string_concatenation
added string interpolation
2 parents 82ac9ed + 7d62492 commit cec53a2

File tree

2 files changed

+26
-18
lines changed

2 files changed

+26
-18
lines changed

README.md

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -818,24 +818,27 @@ String#downcase + ==: 2914767.7 i/s - 1.27x slower
818818

819819
```
820820
$ ruby -v code/string/concatenation.rb
821-
ruby 2.2.0p0 (2014-12-25 revision 49005) [x86_64-darwin14]
821+
ruby 2.2.2p95 (2015-04-13 revision 50295) [x86_64-linux]
822822
823+
Warming up --------------------------------------
824+
String#+ 149.298k i/100ms
825+
String#concat 151.505k i/100ms
826+
String#append 153.389k i/100ms
827+
"foo" "bar" 195.552k i/100ms
828+
"#{'foo'}#{'bar'}" 193.784k i/100ms
823829
Calculating -------------------------------------
824-
String#+ 96.314k i/100ms
825-
String#concat 99.850k i/100ms
826-
String#append 100.728k i/100ms
827-
"foo" "bar" 121.936k i/100ms
828-
-------------------------------------------------
829-
String#+ 2.731M (± 4.6%) i/s - 13.677M
830-
String#concat 2.847M (± 5.2%) i/s - 14.279M
831-
String#append 2.972M (± 6.1%) i/s - 14.807M
832-
"foo" "bar" 4.951M (± 6.2%) i/s - 24.753M
830+
String#+ 2.977M (± 1.1%) i/s - 14.930M in 5.015179s
831+
String#concat 3.017M (± 1.3%) i/s - 15.150M in 5.023063s
832+
String#append 3.076M (± 1.2%) i/s - 15.492M in 5.037683s
833+
"foo" "bar" 5.370M (± 1.0%) i/s - 26.986M in 5.026271s
834+
"#{'foo'}#{'bar'}" 5.182M (± 4.6%) i/s - 25.967M in 5.022093s
833835
834836
Comparison:
835-
"foo" "bar": 4950955.3 i/s
836-
String#append: 2972048.5 i/s - 1.67x slower
837-
String#concat: 2846666.4 i/s - 1.74x slower
838-
String#+: 2730980.7 i/s - 1.81x slower
837+
"foo" "bar": 5369594.5 i/s
838+
"#{'foo'}#{'bar'}": 5181745.7 i/s - same-ish: difference falls within error
839+
String#append: 3075719.2 i/s - 1.75x slower
840+
String#concat: 3016703.5 i/s - 1.78x slower
841+
String#+: 2977282.7 i/s - 1.80x slower
839842
```
840843

841844
##### `String#match` vs `String#start_with?`/`String#end_with?` [code (start)](code/string/start-string-checking-match-vs-start_with.rb) [code (end)](end-string-checking-match-vs-end_with.rb)

code/string/concatenation.rb

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,15 @@ def fast
2020
'foo' 'bar'
2121
end
2222

23+
def fast_interpolation
24+
"#{'foo'}#{'bar'}"
25+
end
26+
2327
Benchmark.ips do |x|
24-
x.report('String#+') { slow_plus }
25-
x.report('String#concat') { slow_concat }
26-
x.report('String#append') { slow_append }
27-
x.report('"foo" "bar"') { fast }
28+
x.report('String#+') { slow_plus }
29+
x.report('String#concat') { slow_concat }
30+
x.report('String#append') { slow_append }
31+
x.report('"foo" "bar"') { fast }
32+
x.report('"#{\'foo\'}#{\'bar\'}"') { fast_interpolation }
2833
x.compare!
2934
end

0 commit comments

Comments
 (0)