Skip to content

Instantly share code, notes, and snippets.

View davepcallan's full-sized avatar

Dave Callan davepcallan

View GitHub Profile
@davepcallan
davepcallan / stringEqualsBenchmarks.cs
Created July 31, 2023 09:24
String equals benchmarks
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Columns;
using BenchmarkDotNet.Configs;
using BenchmarkDotNet.Jobs;
using BenchmarkDotNet.Reports;
using System;
namespace BenchmarkDotNet.Samples
{
[MemoryDiagnoser]
@davepcallan
davepcallan / StringCompareDotNet8Benchmarks.cs
Created August 3, 2023 11:33
.NET 7 v .NET String.Equals OrdinalIgnoreCase benchmarks
using System.Collections.Generic;
using System;
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Columns;
using BenchmarkDotNet.Configs;
using BenchmarkDotNet.Jobs;
using BenchmarkDotNet.Reports;
using BenchmarkDotNet.Environments;
namespace Benchmarks
@davepcallan
davepcallan / listPatternExamples.cs
Created August 3, 2023 16:33
C# 11 List Pattern Examples
namespace ListPatterns
{
internal class Program
{
static void Main(string[] args)
{
int[] numbers = { 1, 2, 3, 4};
Console.WriteLine("constant and relational patterns");
Console.WriteLine(numbers is [1, 2, 3, 4]); // True as exact match
@davepcallan
davepcallan / xUnitDataTests.cs
Created August 9, 2023 16:18
Parameterized Tests with xUnit Example
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using Xunit;
public class StringFormatter
{
public string Reverse(string input)
{
@davepcallan
davepcallan / PerfSensitiveStringConcatenation.cs
Created August 14, 2023 11:57
Performance Sensitive simple string concatenation in .NET 8
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Columns;
using BenchmarkDotNet.Configs;
using BenchmarkDotNet.Jobs;
using BenchmarkDotNet.Order;
using BenchmarkDotNet.Reports;
using System;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
@davepcallan
davepcallan / StackOverflowCloneInsertData.sql
Created August 16, 2023 13:22
Create schema and insert dummy data into Stack Overflow clone
-- Create StackOverflow-like Database
CREATE DATABASE StackOverflowClone
GO
USE StackOverflowClone
GO
-- Users table
@davepcallan
davepcallan / ParamsBenchmarks.cs
Created October 16, 2023 17:25
Benchmarks using the params keyword in C#
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Columns;
using BenchmarkDotNet.Configs;
using BenchmarkDotNet.Jobs;
using BenchmarkDotNet.Reports;
namespace BenchmarkDotNet.Samples
{
[Config(typeof(Config))]
[SimpleJob(RuntimeMoniker.Net80)]
@davepcallan
davepcallan / frozenDictionaryBenchmarks.cs
Last active March 2, 2024 10:06
.NET 8 FrozenDictionary v other Dictionary type benchmarks
using System.Collections.Generic;
using System;
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Columns;
using BenchmarkDotNet.Configs;
using BenchmarkDotNet.Jobs;
using BenchmarkDotNet.Reports;
using System.Collections.Frozen;
using System.Linq;
using System.Collections.ObjectModel;
@davepcallan
davepcallan / globalQueryFilters.cs
Last active September 11, 2024 19:42
Apply global query filters automatically to all entities which implement an interface or have a particular property
public static void ApplyGlobalFilters<TInterface>(this ModelBuilder modelBuilder,
Expression<Func<TInterface,
bool>> expression)
{
foreach (var entityType in modelBuilder.Model.GetEntityTypes())
{
if (entityType.ClrType.GetInterface(typeof(TInterface).Name) != null)
{
var newParam = Expression.Parameter(entityType.ClrType);
@davepcallan
davepcallan / dotnet9LinqBenchmarks.cs
Last active February 8, 2024 19:00
.NET 8 v .NET 9 LINQ benchmarks
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Columns;
using BenchmarkDotNet.Configs;
using BenchmarkDotNet.Environments;
using BenchmarkDotNet.Jobs;
using BenchmarkDotNet.Reports;
namespace Benchmarks
{
[Config(typeof(Config))]