Benchmark:
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Running;
using System.IO;
using System.Text.Json;
[MemoryDiagnoser]
public class Program
{
public static void Main(string[] args) => BenchmarkSwitcher.FromAssembly(typeof(Program).Assembly).Run(args);
private Data _data = new Data { Name = "Stephen", Address = "Here" };
private MemoryStream _stream = new MemoryStream();
private Utf8JsonWriter _writer;
[GlobalSetup]
public void Setup()
{
_writer = new Utf8JsonWriter(_stream);
}
[Benchmark(OperationsPerInvoke = 1000)]
public void Serialize()
{
for (int i = 0; i < 1000; i++)
{
_stream.Position = 0;
_writer.Reset();
JsonSerializer.Serialize(_writer, _data);
}
}
internal class Data
{
public string Name;
public string Address;
}
}
On my machine, running this with:
dotnet run -c Release -f net5.0 --filter ** --join --runtimes net5.0 net6.0
with .NET 5.0.8 and .NET 6.0.0 (6.0.21.40103), I get these results:
| Method |
Job |
Runtime |
Toolchain |
Mean |
Error |
StdDev |
Ratio |
| Serialize |
Job-DDTUUM |
.NET 5.0 |
net5.0 |
85.54 ns |
0.487 ns |
0.407 ns |
1.00 |
| Serialize |
Job-EGSUAL |
.NET 6.0 |
net6.0 |
106.80 ns |
0.328 ns |
0.273 ns |
1.25 |
For me at least, that 25% regression is very consistent.
Benchmark:
On my machine, running this with:
with .NET 5.0.8 and .NET 6.0.0 (6.0.21.40103), I get these results:
For me at least, that 25% regression is very consistent.