Skip to content

Instantly share code, notes, and snippets.

View aalmada's full-sized avatar
🏠
Working from home

Antão Almada aalmada

🏠
Working from home
View GitHub Profile
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
var root = ParallelAll([
Invert(Repeat(WriteLineRange(0, 4), 2)),
WriteLineRange(10, 10),
]);
await foreach(var _ in root)
/*
SharpLab tools in Run mode:
• value.Inspect()
• Inspect.Heap(object)
• Inspect.Stack(value)
• Inspect.MemoryGraph(value1, value2, …)
*/
using System;
using System.Collections.Generic;
using BenchmarkDotNet.Columns;
using BenchmarkDotNet.Configs;
using BenchmarkDotNet.Reports;
namespace DotNetBenchmarks;
class BaseConfig : ManualConfig
{
public BaseConfig()
{
_ = WithSummaryStyle(SummaryStyle.Default.WithRatioStyle(RatioStyle.Trend));
@aalmada
aalmada / MinMaxTest.cs
Created February 13, 2024 12:04
Difference in behavior between Enumerable.Max() and TensorPrimitives.Max()
// @nuget: System.Numerics.Tensors
using System;
using System.Linq;
using System.Numerics.Tensors;
float[] array = [1.0f, float.NaN, 2.0f];
Console.WriteLine($"LINQ Min: {array.Min()}");
Console.WriteLine($"LINQ Max: {array.Max()}");
Console.WriteLine($"Tensors Min: {TensorPrimitives.Min(array)}");
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Columns;
using BenchmarkDotNet.Configs;
using BenchmarkDotNet.Order;
using BenchmarkDotNet.Reports;
using NetFabric.Numerics;
using System.Runtime.InteropServices;
using System.Runtime.CompilerServices;
[Config(typeof(Config))]
@aalmada
aalmada / ForEach.cs
Last active October 1, 2023 21:57
Implementation of the ForEach method for all types of collections using value delegated and other performance optimizations.
using System;
using System.Collections.Generic;
using System.Numerics;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using static System.Runtime.InteropServices.JavaScript.JSType;
namespace NetFabric;
public interface IAction<in T>
@aalmada
aalmada / ForEachBenchmarks-asm.md
Last active July 30, 2023 22:09
The ASM code generated by .NET 8 when foreach is applied to different types of collections

.NET 6.0.20 (6.0.2023.32017), X64 RyuJIT AVX2

; ForEachBenchmarks.Array()
;         var sum = 0;
;         ^^^^^^^^^^^^
       xor       eax,eax
;         foreach (var item in array!)
;                              ^^^^^^
       mov       rdx,[rcx+8]
       xor       ecx,ecx
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Linq;
using System.Runtime.InteropServices;
using BenchmarkDotNet.Attributes;
public class ForEachReferenceBenchmarks
{
string[]? array;
@aalmada
aalmada / MaxByBenchmarks.cs
Last active July 5, 2023 12:12
Benchmarking Enumerable.MaxBy() against Enumerable.OrderByDescending() followed by Enumerable.First()
using System;
using System.Collections.Generic;
using System.Linq;
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Configs;
namespace LinqBenchmarks;
public record Person(int Age);
@aalmada
aalmada / siwe.cs
Last active December 26, 2022 18:28
EIP-4361: Sign-In with Ethereum (SIWE) message generation, parsing and verification in C#/.NET
using System.Globalization;
using System.Text;
using System.Text.RegularExpressions;
using Nethereum.Signer;
using Nethereum.Util;
using NodaTime;
using NodaTime.Text;
namespace Farfetch.Web3;