The C# compiler is smart enough to optimize string concatenation with the +
operator into String.Concat
calls.
The following code:
var a = "one";
var b = "two";
var c = "three";
var d = "four";
var x = a + b + c + d;
Is compiled into this IL:
IL_0000: ldstr "one"
IL_0005: stloc.0 // a
IL_0006: ldstr "two"
IL_000B: stloc.1 // b
IL_000C: ldstr "three"
IL_0011: stloc.2 // c
IL_0012: ldstr "four"
IL_0017: stloc.3 // d
IL_0018: ldloc.0 // a
IL_0019: ldloc.1 // b
IL_001A: ldloc.2 // c
IL_001B: ldloc.3 // d
IL_001C: call System.String.Concat
The compiler figured out the correct overload of String.Concat
that takes 4 arguments and used that.
The F# compiler doesn't do that. Instead, each +
is compiled into a separate call of String.Concat
:
IL_0005: ldstr "one"
IL_000A: ldstr "two"
IL_000F: call System.String.Concat
IL_0014: ldstr "three"
IL_0019: call System.String.Concat
IL_001E: ldstr "four"
IL_0023: call System.String.Concat
Obviously this is because this particular optimization is not implemented in the F# compiler.
The question is why: is it technically hard to do or is there some other reason?
String concatenation is a fairly common operation and while I realize that the performance of compiled code is not a top priority, I imagine this kind of optimization would be useful in many cases.
Summary: Using the string concatenation operator is slightly faster than using format string literals. Unless you are performing many hundreds of thousands of string concatenations and need them done very quickly, the implementation chosen is unlikely to make a difference.
concatenate with f-strings In Python 3.6 and above, use f-strings(formatted string literals) which makes the format() function easier to use. This can be used by simply adding a f or F before the quotes in the string literal. Expand any variable in f-strings , and call any process in {} .
f-strings are faster than both %-formatting and str. format() . At runtime, each expression inside the curly braces gets evaluated within its own scope, and then it's put together into the final string.
I don't think there is anything hard about the optimization - I think the main reason why it is not implemented is that it is specific to string concatenation and does not apply more generally. However, it sounds like something that would be an interesting project using the F# open source release!
That said, even the C# optimization done above is not that clever. There is no reason why the compiler shouldn't just concatenate the strings directly when they are constants and produce:
IL_0000: ldstr "onetwothreefour"
In other words, there is always a tradeoff between adding something that is generally useful and adding more and more special cases - the C# compiler apparently has a few more special cases related to string concatenation...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With