Skip to content

Instantly share code, notes, and snippets.

View badamczewski's full-sized avatar
🙃

Bartosz badamczewski

🙃
View GitHub Profile
@badamczewski
badamczewski / HowToCopyEvents
Created May 15, 2014 20:50
How to copy events shit hack code :) - don't use if you don't know what ur doing.
public class Test
{
public event EventHandler SomeEvent;
public void Do()
{
SomeEvent(this, null);
}
}
@badamczewski
badamczewski / missaligment.cs
Created February 19, 2015 05:49
Misaligment Test .NET
class Program
{
int MAX_NUM_LINES = 1000;
int ALLOC_SIZE = 0;
int LINE_SIZE = 64;
int PG_SIZE = 4096;
int WORD_SIZE = 8;
public Program()
{
@badamczewski
badamczewski / MCSLock
Last active August 29, 2015 14:19
Very simple MCL lock implementation.
#region Licence
/*
Copyright (c) 2011-2015 Bartosz Adamczewski (Bax Services)
MCSLock is distributed WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
@badamczewski
badamczewski / Fast FizzBuzz
Created June 24, 2015 11:03
branchless fizzbuzz.
object[] arr = new object[] { null, "fizz", "buzz", "fizzbuzz" };
int r = 0;
int bf = 1;
int bb = 1;
int a1, a2 = 0;
for (int i = 1; i < 100000000; i++)
{
a1 = a2 = 0;
| Method | Mean | Error | StdDev | Ratio |
|----------- |-----------:|----------:|----------:|------:|
| Linq | 16.8220 ns | 0.4014 ns | 1.1582 ns | 1.00 |
| Last | 0.9100 ns | 0.0678 ns | 0.1797 ns | 0.06 |
| LastBetter | 0.8448 ns | 0.0645 ns | 0.1482 ns | 0.05 |
Code:
public class Benchmark
{
| Method | Mean | Error | StdDev | Ratio | RatioSD |
|------------------------------------- |----------:|----------:|----------:|------:|--------:|
| SkipLinq | 47.386 ns | 0.4797 ns | 0.4487 ns | 1.00 | 0.00 |
| SkipHyperLinq | 31.090 ns | 0.3779 ns | 0.3350 ns | 0.66 | 0.01 |
| SkipLinq_Custom_List | 73.336 ns | 1.3644 ns | 1.2095 ns | 1.55 | 0.03 |
| SkipLinq_Custom_List_2 | 71.604 ns | 1.3169 ns | 1.4090 ns | 1.51 | 0.03 |
| SkipLinq_Custom_Enumerable | 48.463 ns | 0.7997 ns | 0.7089 ns | 1.02 | 0.02 |
| SkipLinq_Custom_Enumerable_AsClass | 40.907 ns | 0.3217 ns | 0.2687 ns | 0.86 | 0.01 |
| SkipLinq_Custom_Enumerable_DF_NoVirt | 2.329 ns | 0.0407 ns | 0.0340 ns | 0.05 | 0.00 |
public static long Measure(Action a)
{
//
// Quick Wormup.
//
Console.WriteLine(" [1] Warm Up ... ");
for (int i = 0; i < 10; i++)
{
a();
public class BranchElimination
{
int[] aTab;
int[] xTab;
int[] xInpTab;
[GlobalSetup]
public void Global()
{
aTab = new int[5] { 10, 20, 30, 40, 50 };
@badamczewski
badamczewski / LoadsPerCycleTest.cs
Created September 21, 2020 05:34
LoadsPerCycleTest
public class CpuLoadLimitTests
{
private readonly int[] _data = new int[100_000_000];
[Benchmark(Baseline = true)]
public unsafe int Sum_Simple()
{
var s0 = 0;
var s1 = 0;
var s2 = 0;
@badamczewski
badamczewski / LoadsPerCycleTest2.cs
Created September 21, 2020 07:31
Loads Per Cycle Test
public class CpuLoadLimitTests
{
private readonly int[] _data = new int[1_000_000];
[Benchmark(Baseline = true)]
[MethodImpl(MethodImplOptions.AggressiveOptimization)]
public unsafe int Sum_Simple()
{
var s0 = 0;
var s1 = 0;