Created
December 4, 2022 14:34
-
-
Save digitaldias/a789f430383e319a9e4f975a89e2585d to your computer and use it in GitHub Desktop.
Playing with strings in .Net6 and 7
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using BenchmarkDotNet.Attributes; | |
using BenchmarkDotNet.Running; | |
using Microsoft.Extensions.Logging; | |
[MemoryDiagnoser(false)] | |
public class Program | |
{ | |
private const string template = "This is the {0} template with {1} valued at {2}"; | |
private readonly ILogger<StringBench> _logger; | |
private readonly ILoggerFactory _loggerFactory = LoggerFactory.Create(builder => | |
{ | |
builder.AddConsole().SetMinimumLevel(LogLevel.Warning); | |
}); | |
private readonly Random _random1; | |
private readonly Random _random2; | |
public Program() | |
{ | |
_random1 = new Random(419); | |
_random2 = new Random(419); | |
_logger = new Logger<StringBench>(_loggerFactory); | |
} | |
public static void Main(string[] args) | |
{ | |
BenchmarkSwitcher.FromAssemblies(new[] { typeof(Program).Assembly }).Run(args); | |
} | |
[Benchmark] | |
public void LogWithParameters() | |
{ | |
var a = _random1.Next(100); | |
var b = _random1.Next(100); | |
var c = _random1.Next(100); | |
_logger.LogInformation(template, a, b, c); | |
} | |
[Benchmark] | |
public void LogWithInterpolation() | |
{ | |
var a = _random2.Next(100); | |
var b = _random2.Next(100); | |
var c = _random2.Next(100); | |
_logger.LogInformation($"This is the {a} template with {b} valued at {c}"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The constructor needs to set a
Logger<Program>
, apologies, remnants :)