Skip to content

Instantly share code, notes, and snippets.

View sonnemaf's full-sized avatar

Fons Sonnemans sonnemaf

View GitHub Profile
@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.
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
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

.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
@sonnemaf
sonnemaf / NoBoundChecks.cs
Last active October 2, 2020 10:29
BM for removing bounds check
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Running;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
namespace ConsoleApp68 {
class Program {
@sonnemaf
sonnemaf / StringPoolTest.cs
Created July 17, 2020 09:48
Test for WindowsCommunityToolkit/Microsoft.Toolkit.HighPerformance/Buffers/StringPool.cs
using Microsoft.Toolkit.HighPerformance.Buffers;
using Microsoft.Toolkit.HighPerformance.Enumerables;
using System;
using System.Buffers.Text;
using System.Diagnostics;
using System.IO;
using System.Text.Unicode;
namespace ImportTest {
class Program {
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Running;
using Microsoft.Toolkit.HighPerformance.Helpers;
using System;
using System.Threading.Tasks;
namespace ToolkitHighPerformanceBM {
class Program {
static void Main(string[] args) {
BenchmarkRunner.Run<BM>();
@sonnemaf
sonnemaf / profiles.json
Created March 17, 2020 17:09
Windows Terminal Profile
// To view the default settings, hold "alt" while clicking on the "Settings" button.
// For documentation on these settings, see: https://aka.ms/terminal-documentation
{
"$schema": "https://aka.ms/terminal-profiles-schema",
"defaultProfile": "{61c54bbd-c2c6-5271-96e7-009a87ff44bf}",
"profiles": [
@sonnemaf
sonnemaf / delegate_invoke_benchmark.cs
Created February 18, 2020 09:03
Source used in a tweet
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Running;
using System;
using System.Linq;
public class Program {
static void Main(string[] args) => BenchmarkRunner.Run(typeof(Program));
private Func<int, int> _delegateToStaticMethod;
private Func<int, int> _delegateToInstanceMethod;
using System;
using System.Diagnostics;
using System.Threading;
using System.Threading.Tasks;
namespace ConsoleApp39 {
class Program {
static void Main(string[] args) {
int total = 0;
var sw = Stopwatch.StartNew();
using System;
using System.Runtime.InteropServices;
namespace Linq4Span {
// QUESTION: Does LINQ for ReadOnlySpan<T> make sense or is the extra Closure class a showstopper?
// QUESTION: Should it only support ReadOnlySpan<T> and not Span<T>
// QUESTION: What about Memory<T> and ReadonlyMemory<T>