Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save davepcallan/fa0cebae7381c65f40588e42eb1c01f6 to your computer and use it in GitHub Desktop.
Save davepcallan/fa0cebae7381c65f40588e42eb1c01f6 to your computer and use it in GitHub Desktop.
Concat 50K one char strings with + and StringBuilder in .NET 7
using System.Text;
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Columns;
using BenchmarkDotNet.Configs;
using BenchmarkDotNet.Jobs;
using BenchmarkDotNet.Reports;
namespace BenchmarkDotNet.Samples
{
[Config(typeof(Config))]
[SimpleJob(RuntimeMoniker.Net70)]
[MemoryDiagnoser]
[HideColumns(Column.RatioSD, Column.AllocRatio, Column.Error, Column.StdDev)]
public class StringConcatInLoop
{
[Params(50, 50000)]
public int Total { get; set; }
[Benchmark(Baseline = true)]
public string Plus()
{
var result = string.Empty;
for (int i = 0; i < Total; i++)
{
result = result + '*';
}
return result;
}
[Benchmark]
public string SB()
{
StringBuilder result = new StringBuilder();
for (int i = 0; i < Total; i++)
{
result.Append('*');
}
return result.ToString();
}
private class Config : ManualConfig
{
public Config()
{
SummaryStyle =
SummaryStyle.Default.WithRatioStyle(RatioStyle.Percentage)
.WithTimeUnit(Perfolizer.Horology.TimeUnit.Millisecond);
}
}
}
}
@davepcallan
Copy link
Author

heading 1

  • 1
  • 2
  • 3

testing

@davepcallan
Copy link
Author

heading1

heading2

heading3

  1. First list item
    • First nested list item
      • Second nested list item

@davepcallan
Copy link
Author

Screenshot of a comment on a GitHub issue showing an image, added in the Markdown, of an Octocat smiling and raising a tentacle.

image above

  • George Washington
  • John Adams
  • Thomas Jefferson

@davepcallan
Copy link
Author

HEADING

@davepcallan
Copy link
Author

testingdavid

@davepcallan
Copy link
Author

heading

heading

heading

@davepcallan
Copy link
Author

heading

@davepcallan
Copy link
Author

abc

@davepcallan
Copy link
Author

heading1

heading2

heading3

@davepcallan
Copy link
Author

tst

@davepcallan
Copy link
Author

hellotest

@davepcallan
Copy link
Author

helloago

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment