Skip to content

Instantly share code, notes, and snippets.

@danielmarbach
Created August 21, 2024 13:52
Show Gist options
  • Save danielmarbach/e2d194f4bff3df4a7170d7c49c7f76a2 to your computer and use it in GitHub Desktop.
Save danielmarbach/e2d194f4bff3df4a7170d7c49c7f76a2 to your computer and use it in GitHub Desktop.
using System.Text;
using BenchmarkDotNet.Attributes;
namespace MicroBenchmarks.Strings;
[SimpleJob]
[MemoryDiagnoser]
public class StringBuilderAppend
{
[Benchmark(Baseline = true)]
public string AppendFormat()
{
var stringBuilder = new StringBuilder();
stringBuilder.AppendFormat("Line {0}\n", 42);
return stringBuilder.ToString();
}
[Benchmark]
public string Append()
{
var stringBuilder = new StringBuilder();
stringBuilder.Append($"Line {42}\n");
return stringBuilder.ToString();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment