Skip to content

Instantly share code, notes, and snippets.

Avatar

Fons Sonnemans sonnemaf

View GitHub Profile
@sonnemaf
sonnemaf / StringConcatSimple.cs
Created February 15, 2023 15:55
Performance benchmark for string interpolations
View StringConcatSimple.cs
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
View settings.json
{
"$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.
View RefStructProblem.cs
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;
View PrimesCounter.razor
<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;
View ForEachRef.cs
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());
@sonnemaf
sonnemaf / Grid.cs
Last active December 1, 2021 15:00
View Grid.cs
using System;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Markup;
namespace ReflectionIT.Universal.Helpers {
/// <summary>
/// <Grid helpers:Grid.RowDefinitions="1*, 50, Auto,2*"
/// helpers:Grid.ColumnDefinitions="1.5*,50,Auto,2.5*" ...
/// </summary>
View IsPrime.cs
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");
@sonnemaf
sonnemaf / RecordStructBenchmark.cs
Created July 27, 2021 15:00
Benchmark showing that a record struct with fields can be faster than with properties. Around 9% faster on my PC. If you change decimal to int they are equally fast.
View RecordStructBenchmark.cs
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Running;
using System;
namespace RecordStructBM {
class Program {
static void Main(string[] args) {
BenchmarkRunner.Run<BM>();
}
}
@sonnemaf
sonnemaf / SieveOfEratosthenesDemo.cs
Created July 2, 2021 15:48
SieveOfEratosthenes
View SieveOfEratosthenesDemo.cs
using System;
using System.Collections;
using static System.Console;
int start = 1, end = 1000;
WriteLine($"The prime numbers between {start} and {end} are :");
BitArray bits = SieveOfEratosthenes(end);
for (int i = start; i <= end; i++)
{
@sonnemaf
sonnemaf / AbusingCSharp.Benchmarks.ArrayChunkBenchmarks-asm.md
Created October 2, 2020 13:15
AbusingCSharp.Benchmarks.ArrayChunkBenchmarks-asm.md
View AbusingCSharp.Benchmarks.ArrayChunkBenchmarks-asm.md

.NET Core 5.0.0 (CoreCLR 5.0.20.45114, CoreFX 5.0.20.45114), X64 RyuJIT

; AbusingCSharp.Benchmarks.ArrayChunkBenchmarks.TestIndexerRef()
       sub       rsp,28
       mov       rax,[rcx+8]
       mov       rax,[rax+8]
       cmp       dword ptr [rax+8],8
       jbe       short M00_L00
       add       rax,30
       add       rsp,28