Performance benchmark references #15240
Replies: 5 comments 7 replies
-
Use IEnumerable when possible and avoid having to convert with
|
Beta Was this translation helpful? Give feedback.
-
Use
|
Beta Was this translation helpful? Give feedback.
-
Creating the same object very often can be costly.In places like As simple as one constant variable saves 12 mb in memory allocation over 100k requests. For example, the below
|
Beta Was this translation helpful? Give feedback.
-
Use string interpolation when concatenation few strings.A benchmark on a medium size string That output of the following benchmark [MemoryDiagnoser]
public class MediumStringConcatenationBenchmarks
{
private const string Phrase1 = "In the vast expanse of the cosmos, stars dance in celestial choreography";
private const string Phrase2 = "weaving tales of cosmic marvels, while on Earth, the tapestry of life unfolds";
private const string Phrase3 = "intertwining moments of joy, sorrow, and discovery, echoing the symphony of existence itself.";
private const string Separator = ", ";
[Benchmark(Baseline = true)]
public string StringConcatenation()
{
var result = string.Empty;
result += Phrase1;
result += Separator;
result += Phrase2;
result += Separator;
result += Phrase3;
return result;
}
[Benchmark]
public string StringConcatenationOneLine()
{
var result = Phrase1 + Separator + Phrase2 + Separator + Phrase3;
return result;
}
[Benchmark]
public string StringInterpolation()
{
var result = $"{Phrase1}{Separator}{Phrase2}{Separator}{Phrase3}";
return result;
}
[Benchmark]
public string StringBuilder()
{
var builder = new StringBuilder();
builder.Append(Phrase1);
builder.Append(Separator);
builder.Append(Phrase2);
builder.Append(Separator);
builder.Append(Phrase3);
return builder.ToString();
}
} But, when we are doing many concatenations, the results are not as good. Here is the benchmark I used [MemoryDiagnoser]
public class SmallStringConcatenationBenchmarks
{
private const string Phrase1 = "One";
private const string Phrase2 = "Two";
private const string Phrase3 = "Three";
private const string Phrase4 = "Four";
private const string Phrase5 = "Five";
private const string Phrase6 = "Six";
private const string Phrase7 = "Seven";
private const string Phrase8 = "Eight";
private const string Phrase9 = "Nine";
private const string Phrase10 = "Ten";
private const char Separator = ' ';
[Benchmark(Baseline = true)]
public string StringConcatenation()
{
var result = string.Empty;
result += Phrase1;
result += Separator;
result += Phrase2;
result += Separator;
result += Phrase3;
result += Separator;
result += Phrase4;
result += Separator;
result += Phrase5;
result += Separator;
result += Phrase6;
result += Separator;
result += Phrase7;
result += Separator;
result += Phrase8;
result += Separator;
result += Phrase9;
result += Separator;
result += Phrase10;
return result;
}
[Benchmark]
public string StringConcatenationOneLine()
{
var result = Phrase1 + Separator
+ Phrase2 + Separator
+ Phrase3 + Separator
+ Phrase3 + Separator
+ Phrase4 + Separator
+ Phrase5 + Separator
+ Phrase6 + Separator
+ Phrase7 + Separator
+ Phrase8 + Separator
+ Phrase9 + Separator
+ Phrase10;
return result;
}
[Benchmark]
public string StringInterpolation()
{
var result = $"{Phrase1}{Separator}{Phrase2}{Separator}{Phrase3}{Separator}{Phrase4}{Separator}{Phrase5}{Separator}{Phrase6}{Separator}{Phrase7}{Separator}{Phrase8}{Separator}{Phrase9}{Separator}{Phrase10}";
return result;
}
[Benchmark]
public string StringBuilder()
{
var builder = new StringBuilder();
builder.Append(Phrase1);
builder.Append(Separator);
builder.Append(Phrase2);
builder.Append(Separator);
builder.Append(Phrase3);
builder.Append(Separator);
builder.Append(Phrase4);
builder.Append(Separator);
builder.Append(Phrase5);
builder.Append(Separator);
builder.Append(Phrase6);
builder.Append(Separator);
builder.Append(Phrase7);
builder.Append(Separator);
builder.Append(Phrase8);
builder.Append(Separator);
builder.Append(Phrase9);
builder.Append(Separator);
builder.Append(Phrase10);
return builder.ToString();
}
} |
Beta Was this translation helpful? Give feedback.
-
These would be great on a docs page BTW. |
Beta Was this translation helpful? Give feedback.
-
This discussion is for performance benchmark tip. Anytime a helpful benchmark is generated, I like to log it here for future references. Feel free to comment with more benchmark results.
@Piedone @sebastienros @Skrypt @hishamco @agriffard
Beta Was this translation helpful? Give feedback.
All reactions