Created
March 29, 2023 23:00
-
-
Save samuelsherrer/3c42a98e0360531665165fe933a9eb52 to your computer and use it in GitHub Desktop.
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; | |
namespace MyBenchmarks | |
{ | |
public class MemoriaBenchmarkDemo | |
{ | |
private readonly List<Boleto> Boletos; | |
public MemoriaBenchmarkDemo() | |
{ | |
Boletos = new List<Boleto>(); | |
for (int i = 0; i < 50; i++) | |
{ | |
Boletos.Add(new Boleto(i, i)); | |
} | |
} | |
[Benchmark] | |
public void Dictionary() | |
{ | |
var boletosPorParcela = Boletos?.ToDictionary(p => p.NumeroParcela); | |
for (int numParcela = 1; numParcela <= 10; numParcela++) | |
{ | |
if (boletosPorParcela != null && boletosPorParcela.TryGetValue(numParcela, out Boleto boletoExistente)) | |
{ | |
continue; | |
} | |
} | |
} | |
[Benchmark] | |
public void Any() | |
{ | |
for (int numParcela = 1; numParcela <= 10; numParcela++) | |
{ | |
if (Boletos.Any(a => a.NumeroParcela == numParcela)) | |
{ | |
continue; | |
} | |
} | |
} | |
[Benchmark] | |
public void Exists() | |
{ | |
for (int numParcela = 1; numParcela <= 10; numParcela++) | |
{ | |
if (Boletos.Exists(a => a.NumeroParcela == numParcela)) | |
{ | |
continue; | |
} | |
} | |
} | |
} | |
public class Program | |
{ | |
public static void Main(string[] args) | |
{ | |
var summary = BenchmarkRunner.Run<MemoriaBenchmarkDemo>(); | |
} | |
} | |
class Boleto | |
{ | |
public Boleto(int id, int numeroParcela) | |
{ | |
Id = id; | |
NumeroParcela = numeroParcela; | |
} | |
public Boleto() | |
{ | |
} | |
public int Id { get; set; } | |
public int NumeroParcela { get; set; } | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment