Skip to content

Instantly share code, notes, and snippets.

View sonnemaf's full-sized avatar

Fons Sonnemans sonnemaf

View GitHub Profile
@sonnemaf
sonnemaf / Program.cs
Created October 30, 2024 09:33
Benchmark LINQ First(…) vs Where(…).First() vs Find() in using .NET9, .NET8 and .NET4.8
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Jobs;
using BenchmarkDotNet.Running;
BenchmarkRunner.Run<BM>();
[HideColumns("Job", "StdDev", "RatioSD", "Alloc Ratio")]
[MemoryDiagnoser(displayGenColumns: false)]
[SimpleJob(RuntimeMoniker.Net90)]
[SimpleJob(RuntimeMoniker.Net80)]
Foo("123,456,7890");
Console.WriteLine();
Bar("123,456,7890");
static void Foo(ReadOnlySpan<char> span) {
Console.WriteLine(span.SplitNext(',').ToString()); // 123
Console.WriteLine(span.SplitNext(',').ToString()); // 456
Console.WriteLine(span.SplitNext(',').ToString()); // 7890
}
@sonnemaf
sonnemaf / Program.cs
Last active September 28, 2023 09:18
ToList(capacity) Benchmark
using BenchmarkDotNet.Running;
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Jobs;
BenchmarkRunner.Run<BM>();
[SimpleJob(RuntimeMoniker.Net80, baseline: true)]
[SimpleJob(RuntimeMoniker.Net70)]
[SimpleJob(RuntimeMoniker.Net48)]
[MemoryDiagnoser(displayGenColumns: false)]
@sonnemaf
sonnemaf / MainPage.xaml
Created September 14, 2023 08:44
Uno Test App used in Twitter video
<Page x:Class="UnoApp8.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="using:UnoApp8"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
Background="{ThemeResource ApplicationPageBackgroundThemeBrush}"
mc:Ignorable="d">
<Grid>
@sonnemaf
sonnemaf / StringConcatSimple.cs
Created February 15, 2023 15:55
Performance benchmark for string interpolations
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Columns;
using BenchmarkDotNet.Jobs;
using BenchmarkDotNet.Running;
using BenchmarkDotNet.Samples;
using System;
using System.Text;
//var s = new StringConcatSimple();
//Console.WriteLine(s.StringInterpolation());
@sonnemaf
sonnemaf / settings.json
Created October 22, 2022 10:36
My Windows Terminal settings
{
"$help": "https://aka.ms/terminal-documentation",
"$schema": "https://aka.ms/terminal-profiles-schema",
"actions":
[
{
"command":
{
"action": "closeTab"
},
@sonnemaf
sonnemaf / RefStructProblem.cs
Created July 13, 2022 10:39
Unhandled exception. System.TypeLoadException: A ByRef-like type cannot be used as the type for an instance field in a non-ByRef-like type.
var line = new Line(new PointStruct(1, 2), new PointStruct(3, 4));
line.Start.Swap();
Console.WriteLine(line);
record struct Line {
private ref PointStruct _start;
private ref PointStruct _stop;
public ref PointStruct Start => ref _start;
<h1>Primes Counter</h1>
<p>@_output</p>
<button class="btn btn-primary" @onclick="CountPrimesSingleThreaded">Single Threaded</button>
<button class="btn btn-primary" @onclick="CountPrimesMultiThreaded">Multi Threaded</button>
@code {
private string _output = "Not yet executed";
private void CountPrimesSingleThreaded() {
int total = 0;
var l = new List<PointStruct>() {
new PointStruct(1, 10),
new PointStruct(2, 20),
new PointStruct(3, 30),
};
var a = l.ToArray();
l.ForEachRef(static (ref PointStruct p) => p.Swap());
var sw = System.Diagnostics.Stopwatch.StartNew();
int total = 0;
for (int i = 1; i <= 20_000_000; i++) {
if (IsPrime(i)) {
total++;
}
}
sw.Stop();
Console.WriteLine($"{total} in {sw.Elapsed} sec");