Skip to content

Instantly share code, notes, and snippets.

View admir-live's full-sized avatar

Admir Mujkic admir-live

View GitHub Profile
@admir-live
admir-live / LinqQueryPerformanceBenchmark.cs
Created June 20, 2024 20:35
Avoid Using the "let" Keyword in LINQ Queries
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Running;
public class LinqQueryPerformanceBenchmark
{
private List<Person> _peopleList;
public LinqQueryPerformanceBenchmark()
{
_peopleList = new List<Person>();
@admir-live
admir-live / LinqPerformanceBenchmark.cs
Created June 19, 2024 21:41
Getting the last value of a collection
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Running;
using System.Collections.Generic;
using System.Linq;
using BenchmarkDotNet.Order;
// Represents a person with an ID and a Name
public class Person
{
public int Id { get; set; }
using MediatR;
using System;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
public class TransactionBehavior<TRequest, TResponse> : IPipelineBehavior<TRequest, TResponse>
{
private readonly DbContext _dbContext;
@admir-live
admir-live / QueryExamplesEfCore9.cs
Created May 29, 2024 03:47
EF Core 9 performance with inlined uncorrelated subqueries
namespace NewInEfCore9;
public static class QueryExamples
{
public static Task RunUserQuerySample()
{
return ExecuteUserQueryTest<UserManagementDbContext>();
}
private static async Task ExecuteUserQueryTest<TContext>()
@admir-live
admir-live / ObjectPoolBenchmarks.cs
Created May 21, 2024 18:21
Object Pool Benchmark Results
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Configs;
using BenchmarkDotNet.Running;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.ObjectPool;
using System;
using System.Linq;
using System.Security.Cryptography;
using System.Threading.Tasks;
@admir-live
admir-live / ClassBenchmark.cs
Created May 20, 2024 13:05
Understanding Class Performance in C#
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Running;
namespace ClassBenchmark
{
public class StandardClass
{
public int StandardProperty { get; set; }
public int GetStandardProperty() => StandardProperty;
@admir-live
admir-live / Number.cs
Created May 15, 2024 08:15
Number.cs
public interface INumber
{
object NumericValue { get; set; }
}
public class Number : INumber
{
public object NumericValue { get; set; }
}
@admir-live
admir-live / GenericHolderNull.cs
Created May 15, 2024 07:44
GenericHolderNull.cs
var intHolder = new Number<int?>(null); // T is int?, which can be null
var stringHolder = new Number<string>("Hello"); // T is string
var objectHolder = new Number<object>(new object()); // T is object
var nullableHolder = new Number<int?>(5); // T is int?, with a value
Console.WriteLine(intHolder.Value); // Output: null
Console.WriteLine(stringHolder.Value); // Output: Hello
Console.WriteLine(objectHolder.Value); // Output: System.Object
Console.WriteLine(nullableHolder.Value); // Output: 5
@admir-live
admir-live / TransactionAnalysisBenchmarkLinqAndPLinq.cs
Created May 14, 2024 05:34
This C# file benchmarks various LINQ and PLINQ methods for analyzing potentially fraudulent transactions in different dataset sizes.
using System.Security.Cryptography;
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Reports;
using BenchmarkDotNet.Running;
public class Transaction
{
public decimal Amount { get; }
public TimeSpan TransactionTime { get; }
using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Threading.Tasks;
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Running;
BenchmarkRunner.Run<PersonAgeSumPerformanceBenchmark>();
[MemoryDiagnoser]