Skip to content

Instantly share code, notes, and snippets.

@overing
overing / BenckmarkDapperImmedAnonType.cs
Last active December 3, 2024 09:05
一個 Dapper 搭配 Func<T> 樣板工廠進行即席匿名類別查詢的性能測試 笑死 匿名更快
using System.ComponentModel.DataAnnotations.Schema;
using System.Reflection;
using BenchmarkDotNet.Attributes;
using Dapper;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using Microsoft.EntityFrameworkCore.Metadata.Conventions;
using Microsoft.Extensions.DependencyInjection;
namespace Benchmarks;
@overing
overing / BenchmarkSqlBuildWay.cs
Last active November 29, 2024 01:43
Performance test using simplified version of ValueStringBuilder on SQL statement string concatenation
using System.Buffers;
using System.Dynamic;
using System.Runtime.CompilerServices;
using System.Text;
using BenchmarkDotNet.Attributes;
namespace Benchmarks;
/* record
| Method | Mean | Error | StdDev | Gen0 | Gen1 | Allocated |
@overing
overing / Program.cs
Last active April 24, 2024 06:06
basic auth file server
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Net.Http.Headers;
using System.Reflection;
using System.Security.Authentication;
using System.Security.Claims;
using System.Security.Cryptography;
@overing
overing / Program.cs
Created April 11, 2024 03:37
乘法溢位範圍列表
using System;
using 計算單位 = uint;
using 驗證單位 = ulong; // 需要比計算單位大
Console.WriteLine($"計算單位: {typeof(計算單位).Name} (Max: {計算單位.MaxValue}), 驗證單位: {typeof(驗證單位).Name}");
for (計算單位 乘數 = 2; 乘數 <= (計算單位.MaxValue / 2); 乘數 += (計算單位)Math.Pow(10, Math.Floor(Math.Log10(乘數))))
{
for (計算單位 被乘數 = 1; 被乘數 < 計算單位.MaxValue; 被乘數++)
{
var total = (計算單位)(被乘數 * 乘數);
#define 有提供存取成員的公開屬性
using System;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using BenchmarkDotNet.Attributes;
using TestDatas;
/*
// * Summary *
@overing
overing / WebSocketOrleansChatRoomDemo.cs
Created November 14, 2023 16:20
展示用 ASP.NET Core 的 WebSocket 與 Orleans 製作簡易的聊天室功能, 當中包含使用 IGrainObserver 來達到由 Grain 主動呼叫前端的使用方式
using System;
using System.IO;
using System.Net.WebSockets;
using System.Text;
using System.Text.Json;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection;
@overing
overing / JsonExtensions.cs
Created November 9, 2023 06:45
Easy to deserialize JSON with anonymous object
using System;
var json = """
{
"IntVal": 3,
"Inner":
{
"FloatVal": 9.8,
"Message": "Test"
},
@overing
overing / Main.cs
Last active November 2, 2023 10:07
從同步上下文捕捉 TaskScheduler 用來將外線中的工作送回主線
using System;
using System.Threading;
using System.Threading.Tasks;
using UnityEngine;
public sealed class Main : MonoBehaviour
{
static int ThreadId => Environment.CurrentManagedThreadId;
GameObject _object;
@overing
overing / BuildTargetGroupExtensions.cs
Last active October 26, 2023 09:27
取得有效的 BuildTargetGroup 替代名稱
using System;
using System.Collections.Generic;
using System.Linq;
using NUnit.Framework;
using UnityEditor;
public sealed class EnumTest
{
[Test]
[TestCase("iOS", true, "iOS")] // iOS 有效
@overing
overing / DynamicEqualityComparison.cs
Last active October 26, 2023 02:25
物件值比較器, 0 GC Alloc, 比 Json 比法快了最少 16x, 亦可調整成比較 field 而不是 property, 寫再多成員都只要呼叫 Comparer.IsEqual(x, y) 即可
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using static System.Linq.Expressions.Expression;
public static class Comparer
{
static readonly Dictionary<Type, Func<object, object, bool>> s_cache = new();