In another blog we compared string concatention and interpolationin Ruby. In that blog, I claimed that “generally, you’ll want to stick with interpolation, as it’s quicker”. However, I didn’t really elaborate or provide any benchmarks. I noticed the post was linked in a reddit comment about this very topic, and I figured I needed to provide an update. So let’s do some benchmarking!
We can use
benchmark from the Ruby standard library to easily accomplish our
goal of comparing the performance of string concatenation and string
interpolation in various scenarios.
Here’s the code for our test:
So, in the above, we have three tests for both methods.
We have the standard case, were we build the string “everybody wang chung tonight” wiht both methods. We use literal strings everywhere, not variables. These tests can be found in the code under
Next, I wanted to see if there was any difference if the word strings for our phrase above were stored in variables. You can find these in teh code as
Lastly, I wanted to test a more modest case. I decided to concatenate two string varables, and interpolate one string variable into another literal string. My understanding is that concatenation takes longer because it makes an intermediate string for each addend. It would follow that if I were only concating a few strings, this affect would be less pronounced. It could be that concatenation beats interpolation in this case. Anyways, let’s run the code and find out.
$ ruby ./benchmark_strings.rb | column -t Concatenation: 0.308086 0.000000 0.308086 ( 0.308712) Var-Concatenation: 0.300441 0.000000 0.300441 ( 0.300878) 2-string-Concatenation: 0.045168 0.000000 0.045168 ( 0.045190) Interpolation: 0.027193 0.000000 0.027193 ( 0.027206) Var-Interpolation: 0.187106 0.000000 0.187106 ( 0.187194) 2-string-(one-var)-Interpolation: 0.051957 0.000000 0.051957 ( 0.051982) $ ruby -v ruby 2.6.5p114 (2019-10-01 revision 67812) [x86_64-linux]
So, what does this tell us?
- Well, interpolation clearly wins with the longer string cases.
- Variables vs. literals has a much more pronounced effect on interpolation than concatenation, with interpolation increasing in runtime by almost a full order of magnitude.
- The 2-string cases are really really close for both. I ran this a few times and they were always close. It seemed like interpolation was more often lower, so I decided to increase the loop count and run it again with 50,000,000 iterations instead of 500,000:
$ ruby ./benchmark_strings2.rb | column -t 2-string-Concatenation: 4.485134 0.000782 4.485916 ( 4.487549) 2-string-(one-var)-Interpolation: 4.760736 0.000000 4.760736 ( 4.766114)
Well, what do you know?! I’m glad I ran it again, because this convinces me that concatenating 2 strings is faster than interpolating them. However, the difference is so negligible that I will probably just pick whatever I think is more readable.
So, in summary:
- Interpolation is more efficient if you’re working with lots of strings.
- If you just have a few strings, concatenation might be slightly faster.
Other things to consider:
- Interpolation can be easier to extend/modify in the future.
- Using interpolation for a literal and a variable is cool, but interpolating 2 variables looks silly if you aren’t including any literal strings: