Skip to content

Instantly share code, notes, and snippets.

@mattwarren
mattwarren / GuidCtorBenchmark.cs
Last active February 14, 2017 17:19
Benchmark of the costs of different ways of creating GUIDs
// From https://twitter.com/aarnott/status/701653964823990273
public class Framework_Guid
{
[Benchmark(Baseline = true)]
public Guid CtorWithHexValues()
{
// creates a Guid that corresponds to "12345678-000b-000c-0001-020304050607".
return new Guid(0x12345678, 0xb, 0xc, 0, 1, 2, 3, 4, 5, 6, 7);
}
@mattwarren
mattwarren / Interpolated_vs_ToString.md
Last active December 20, 2016 00:00
Benchmark of C# 6 String Interpolation vs calling ToString() directly
// Inspired by https://twitter.com/Nick_Craver/status/702693060472414208
public class Framework_Interpolated_vs_ToString
{
	private long counter = 0;

	[Setup]
	public void Setup()
	{
 counter = 0;
@mattwarren
mattwarren / SampleBenchmarkDotNetLog.txt
Last active December 20, 2016 00:00
Sample BenchmarkDotNet Log
// ***** BenchmarkRunner: Start *****
// Found benchmarks:
// IntroBasic_SleepWithDescription
// IntroBasic_Sleep
// **************************
// Benchmark: IntroBasic_SleepWithDescription
// *** Generate ***
// Result = Success
// DirectoryPath = C:\__GitHub__\BenchmarkDotNet\BenchmarkDotNet.Samples\bin\Release\IntroBasic_SleepWithDescription

From this Benchmark code:

[Benchmark]
public Dictionary<string, string> DictionaryEnumeration()
{
	// Doesn't allocate
	foreach (var item in dict)
	{
		;
	}
@mattwarren
mattwarren / Explanation.md
Last active December 20, 2016 00:00
Sparkline SVG Optimisation

As per the spec:

  • The command letter can be eliminated on subsequent commands if the same command is used multiple times in a row (e.g., you can drop the second "L" in "M 100 200 L 200 100 L -100 -200" and use "M 100 200 L 200 100 -100 -200" instead).

Original SVG - 4.93 KB (5,049 bytes)

<svg version="1.1" baseProfile="full" width="500" height="50" xmlns="http://www.w3.org/2000/svg" preserveAspectRatio="none">
  <line x1="0" y1="50" x2="500" y2="50" stroke="#f6f6f6" stroke-width="1" />
  <g fill="#008cba" stroke="none">
    <path d="M0 50 L0 41.7 L1 41.5 L2 42.2 L3 43.5 L4 44.3 L5 44.7 L6 45.2 L7 43.5 L8 44.3 L9 42.7 L10 42.3 L11 43.5 L12 44.5 L13 44.3 L14 44.3 L15 42.7 L16 43.0 L17 41.3 L18 40.8 L19 40.3 L20 41.8 L21 41.3 L22 42.8 L23 44.0 L24 42.7 L25 43.2 L26 43.8 L27 44.5 L28 43.2 L29 42.8 L30 44.0 L31 44.2 L32 43.3 L33 42.0 L34 42.5 L35 41.0 L36 39.5 L37 40.0 L38 40.5 L39 41.0 L40 40.7 L41 41.2 L42 41.7 L43 40.0 L44 41.0 L45 41.7 L46 41.0 L47 4
@mattwarren
mattwarren / ImmutabilityBenchmarks.cs
Last active December 20, 2016 00:00
Some Benchmarks for Jon Skeets Immutability Code (see https://github.com/jskeet/DemoCode/tree/master/Immutability)
using BenchmarkDotNet;
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Running;
using System.Collections.Generic;
using System.Collections.Immutable;
namespace ImmutabilityBenchmark
{
//[Config(processCount: 1, warmupIterationCount: 1, targetIterationCount: 1)]
public class BenchmarkProgram
@mattwarren
mattwarren / ObjectPool.cs
Created January 13, 2016 14:33 — forked from benaadams/ObjectPool.cs
Object Pools
public interface IObjectPool<T>
{
T Rent();
void Return(T obj);
}
public interface IResetable
{
void Reset();
}
module BenchmarkSpec
open BenchmarkDotNet
open NUnit.Framework
open FsUnit
open BenchmarkDotNet.Tasks
open System.Threading
type File =
{ Name : string
<Query Kind="Program">
<NuGetReference>Octokit</NuGetReference>
<NuGetReference>Octokit.Reactive</NuGetReference>
<NuGetReference>Rx-Main</NuGetReference>
<Namespace>Octokit</Namespace>
<Namespace>System.Linq</Namespace>
<Namespace>System.Reactive.Linq</Namespace>
<Namespace>System.Threading.Tasks</Namespace>
</Query>
@mattwarren
mattwarren / StringBuilderEfficientAppend.cs
Created November 27, 2015 14:17
A StringBuilder Append method without tempoary string allocations - see https://github.com/dotnet/corefx/issues/2102 for more info
[BenchmarkTask(processCount: 1)]
public unsafe class StringBuilderEfficientAppend
{
[Params(1, 5, 10, 20, 50)]
public int StringLength;
private byte[] UTF8Bytes;
private byte* bytes;
private IntPtr unmanagedPointer;