Last active
March 7, 2016 15:19
-
-
Save HaloFour/d2ca5c056ba1139245f0 to your computer and use it in GitHub Desktop.
Decomposition Microbenchmark
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.Diagnostics; | |
public struct ValueTuple<T1, T2> { | |
public T1 Item1; | |
public T2 Item2; | |
public ValueTuple(T1 item1, T2 item2) { | |
this.Item1 = item1; | |
this.Item2 = item2; | |
} | |
} | |
public class Record { | |
public int X { get; } | |
public int Y { get; } | |
public Record(int x, int y) { | |
this.X = x; | |
this.Y = y; | |
} | |
public void Decompose(out int x, out int y) { | |
x = this.X; | |
y = this.Y; | |
} | |
public ValueTuple<int, int> DecomposeTuple() { | |
return new ValueTuple<int, int>(X, Y); | |
} | |
public static bool Decompose(Record record, out int x, out int y) { | |
if (record == null) { | |
x = 0; | |
y = 0; | |
return false; | |
} | |
x = record.X; | |
y = record.Y; | |
return true; | |
} | |
public static ValueTuple<int, int>? DecomposeTuple(Record record) { | |
if (record == null) { | |
return null; | |
} | |
return new ValueTuple<int, int>(record.X, record.Y); | |
} | |
} | |
static class Program { | |
static void Main() { | |
const int ITERATIONS = 1000000000; | |
Stopwatch stopwatch = new Stopwatch(); | |
Record record = new Record(2, 3); | |
stopwatch.Start(); | |
for (int i = 0; i < ITERATIONS; i++) { | |
bool result = (record.X == 5 && record.Y == 3); | |
} | |
stopwatch.Stop(); | |
Console.WriteLine($"Properties: {stopwatch.Elapsed}"); | |
stopwatch.Restart(); | |
for (int i = 0; i < ITERATIONS; i++) { | |
int x, y; | |
record.Decompose(out x, out y); | |
bool result = (x == 5 && y == 3); | |
} | |
stopwatch.Stop(); | |
Console.WriteLine($"Inst Decom: {stopwatch.Elapsed}"); | |
stopwatch.Restart(); | |
for (int i = 0; i < ITERATIONS; i++) { | |
ValueTuple<int, int> tuple = record.DecomposeTuple(); | |
bool result = (tuple.Item1 == 5 && tuple.Item2 == 3); | |
} | |
stopwatch.Stop(); | |
Console.WriteLine($"Inst Tuple: {stopwatch.Elapsed}"); | |
stopwatch.Restart(); | |
for (int i = 0; i < ITERATIONS; i++) { | |
int x, y; | |
Record.Decompose(record, out x, out y); | |
bool result = (x == 5 && y == 3); | |
} | |
stopwatch.Stop(); | |
Console.WriteLine($"Stat Decom: {stopwatch.Elapsed}"); | |
stopwatch.Restart(); | |
for (int i = 0; i < ITERATIONS; i++) { | |
ValueTuple<int, int>? tuple = Record.DecomposeTuple(record); | |
bool result = (tuple != null && tuple.Value.Item1 == 5 && tuple.Value.Item2 == 3); | |
} | |
stopwatch.Stop(); | |
Console.WriteLine($"Stat Tuple: {stopwatch.Elapsed}"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment