Skip to content

Instantly share code, notes, and snippets.

@samuelsherrer
Created March 29, 2023 23:00
Show Gist options
  • Save samuelsherrer/3c42a98e0360531665165fe933a9eb52 to your computer and use it in GitHub Desktop.
Save samuelsherrer/3c42a98e0360531665165fe933a9eb52 to your computer and use it in GitHub Desktop.
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