Interpolated strings vs Concatenated strings
Asked Answered
C

0

6

How can I test the differences in scenarios of using interpolated strings vs concatenating strings such as this example:

String interpolation

const string a = "I change per class or method";
string b = $"abcdefghijklmnopqrstuvw {a} xyz";
Console.WriteLine(b);

IL_0000:  ldstr       "abcdefghijklmnopqrstuvw {0} xyz"
IL_0005:  ldstr       "I change per class or method"
IL_000A:  call        System.String.Format
IL_000F:  call        System.Console.WriteLine
IL_0014:  ret         

This will hopefully store a single value of a via string interning for all of the code but the cost of using string.Format may cancel out benefits of storing a single, small string.

Concatenation

const string a = "I change per class or method";
string b = "abcdefghijklmnopqrstuvw " + a + " xyz";
Console.WriteLine(b);

IL_0000:  ldstr       "abcdefghijklmnopqrstuvw I change per class or method xyz"
IL_0005:  call        System.Console.WriteLine
IL_000A:  ret         

This will store many different strings since it looks like they're concatenated during compile time, so you will have many "similar" copies of this same string except the a part of it will be different. The advantage is that there is no string.Format call.


How can I evaluate the benefits of doing it one way vs the other way? Do benchmarks for this type of thing already exist?

Cuculiform answered 24/7, 2018 at 19:41 Comment(12)
It probably depends on what you are measuring. Is it running memory footprint? Is it speed at which you generate the resulting strings (like in a loop for example)? Your 2nd concatenation example could also be varied if the strings are not constants which is usually the case (coming from input etc) which would not be compiled into a single string like the IL you have there. What I am getting at is that scenarious vary and you might have to test each "block of code" that you want to make "more performant".Novella
For the examples listed, IMHO, the difference is negligible. Is this part of a specific project? Are you seeing a performance issue? At this level, you might get an "it depends" answer.Aldridge
This is super tough, because...particularly when using non-desktop versions of the framework (Such as Compact Framework), the GC can actually end up being the most important part of the performance equation. We ended up measuring overall performance of two different compiles where the only thing that changed was the string-handling. Gave us a very clear answer. (for our use case)Bunde
You are probably right that the 2nd example would likely not be a constant in most of my cases, so it would concatenate during runtimeCuculiform
@Bunde That is the type of answer I'm looking for I think. This would be for asp.net core api most likely so GC is probably the most important aspectCuculiform
@JoePhillips - Unfortunately I forget the profiling details, which are probably irrelevant to .NET core anyway. I remember the result which is that the more "expensive" operation resulted in less fragmented memory, and significantly improved the overall performance in the relevant areas. But of course that may not be true in this case.Bunde
Make a in runtime: string a = new string('I', 1) + " change per class or method";. So b cannot be concatenate in compile time.Annorah
@AlexanderPetrov That actually adds some extra overhead. I think removing the const accomplishes what you wanted. Although then it interns strings "abc" and "xyz" separately. I'm starting to see there are a lot of combinations of how this could end up being more or less efficient... hrmmCuculiform
If you are doing it once, you'll spend much more "money" thinking about the issue than the "money" you'll save by doing any kind of optimization. If you are doing this in a tight loop, then you need to care about things like the GC (most string operations spin off a lot of garbage). Read-up on System.Text.StringBuilder - it's the right tool to use when concatenating strings very often. "Money" is in quotes because I'm talking about the overall cost of design/implementation versus run time - it's a bit like comparing apples and aardvarks, but, you still can.Cardon
If your question is about micro-benchmarking, the answer is usually BenchmarkDotNet. For macro-benchmarking, nothing beats profiling an actual application. For either, armchair reasoning is the least effective way to draw conclusions.Hairspring
@JoePhillips How can I learn the assembly/machine language values that you found out in your question?Unknow
@Unknow I believe I was using LINQPad at the time. It shows them in thereCuculiform

© 2022 - 2024 — McMap. All rights reserved.