This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var config = ManualConfig.Create(DefaultConfig.Instance); | |
var gcDiagnoser = new MemoryDiagnoser(); | |
config.Add(new Job { Mode = Mode.SingleRun, LaunchCount = 2, WarmupCount = 2, TargetCount = 10 }); | |
config.Add(gcDiagnoser); | |
_________________________________ Patch for BenchMarkDotNet to have precise GC: | |
diff --git a/BenchmarkDotNet/Running/MethodInvoker.cs b/BenchmarkDotNet/Running/MethodInvoker.cs | |
index e8d8de5..6a46aa2 100644 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Runtime.InteropServices; | |
namespace EvilArray | |
{ | |
/// <summary> | |
/// Cast an array of structs to an array of byte[] | |
/// </summary> | |
class Program | |
{ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Related to this benchmark article https://stebet.net/benchmarking-and-performance-optimizations-in-c-using-benchmarkdotnet/ | |
public static class IP2Locationxoofx | |
{ | |
public static uint IPAddressToInteger(string input) | |
{ | |
uint ipAddress = 0; | |
uint acc = 0; | |
// Note: we assume that the string is well formed | |
foreach (var c in input) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Follow up of the discussion https://twitter.com/stebets/status/739563007202758656 | |
public static unsafe string IntegerToIPAddress(uint input) | |
{ | |
var text = stackalloc char[15]; | |
var ptext = text + 15; | |
for (int i = 0; i < 4; i++) | |
{ | |
if (i > 0) | |
{ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Follow up of https://twitter.com/stebets/status/739563007202758656 | |
private static readonly string[] ByteToString = Enumerable.Range(0, 256).Select(x => x.ToString()).ToArray(); | |
public unsafe static string FastIntegerToIPAddress2(uint input) | |
{ | |
// t3.t2.t1.t0 | |
var t0 = ByteToString[input & 0xFF]; | |
input = input >> 8; | |
var t1 = ByteToString[input & 0xFF]; | |
input = input >> 8; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Check the performance of using an Array (preallocated) vs a List<T>, vs a Dictionary<string, T> | |
// both in terms of CPU and memory | |
// * Summary * | |
// | |
// BenchmarkDotNet=v0.9.7.0 | |
// OS=Microsoft Windows NT 6.2.9200.0 | |
// Processor=Intel(R) Core(TM) i7-4770 CPU 3.40GHz, ProcessorCount=8 | |
// Frequency=3319351 ticks, Resolution=301.2637 ns, Timer=TSC | |
// HostCLR=MS.NET 4.0.30319.42000, Arch=32-bit RELEASE | |
// JitModules=clrjit-v4.6.1080.0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Ugly pure ASM version of https://github.com/aspnet/KestrelHttpServer/pull/1138 and gist https://gist.github.com/benaadams/2dd3f99230757111e91915f638067a09 | |
// Not performing better though... | |
[Benchmark] | |
public int TestFindFirstByteAVX() | |
{ | |
var vector = _vectors[ByteSet]; | |
return FindFirstByteAVX(ref vector); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Q: At which size is it preferrable to use binary search over a simple linear search for a small ordered set? | |
R: Under 5 elements, linear search is slightly faster (from 200% to 10% faster) | |
But in practice, not sure a switch case to select the best method is really worth it | |
Unless main usecase is having most of the time only 1-2 elements (so it could be optimized manually without a loop and switch to binary otherwise) | |
At 3-4 elements, linear is only 10-5% faster | |
Relative performance between linear and binary search: | |
size x86 x64 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// You need to configure your C# project with x86 or x64 platform (Tools\Configuration Manager\Create new Platform on the project) | |
// otherwise the native libSkiaSharp.dll will not get copied | |
using System; | |
using System.IO; | |
using SkiaSharp; | |
namespace TestSkia | |
{ | |
class Program | |
{ |