Last active
January 17, 2019 06:50
-
-
Save Nyconing/eac07abfd51b98e8dc9e4f886f96282d to your computer and use it in GitHub Desktop.
TupleBenchmarking
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.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
using System.Threading.Tasks; | |
namespace TupleBenchmarking | |
{ | |
using System; | |
using System.Collections.Generic; | |
using System.Diagnostics; | |
using System.Runtime.CompilerServices; | |
class Program | |
{ | |
static void Main() | |
{ | |
Console.WriteLine("Warming"); | |
Allocation(); | |
Argument(); | |
Return(); | |
Load(); | |
ComparisonEquals(); | |
ComparisonHashCode(); | |
Console.Clear(); | |
Allocation(); | |
Argument(); | |
Return(); | |
Load(); | |
ComparisonEquals(); | |
ComparisonHashCode(); | |
Console.ReadLine(); | |
} | |
public class UseClass { | |
public string Item1; | |
public string Item2; | |
} | |
static void Allocation() | |
{ | |
// Time allocating the object. | |
const int max = 1000000; | |
var a = new Tuple<string, string>("", ""); | |
var b = new KeyValuePair<string, string>("", ""); | |
var c = ("", ""); | |
var d = new UseClass {Item1 = "", Item2 = ""}; | |
var s1 = Stopwatch.StartNew(); | |
for (var i = 0; i < max; i++) | |
{ | |
var tuple = new Tuple<string, string>("cat", "dog"); | |
} | |
s1.Stop(); | |
var s2 = Stopwatch.StartNew(); | |
for (var i = 0; i < max; i++) | |
{ | |
var pair = new KeyValuePair<string, string>("cat", "dog"); | |
} | |
s2.Stop(); | |
var s3 = Stopwatch.StartNew(); | |
for (var i = 0; i < max; i++) | |
{ | |
var pair = ("cat", "dog"); | |
} | |
s3.Stop(); | |
var s4 = Stopwatch.StartNew(); | |
for (var i = 0; i < max; i++) { | |
var pair = new UseClass {Item1 = "cat", Item2 = "dog"}; | |
} | |
s4.Stop(); | |
Console.WriteLine(((double)(s1.Elapsed.TotalMilliseconds * 1000000) / max) + " allocation, Tuple"); | |
Console.WriteLine(((double)(s2.Elapsed.TotalMilliseconds * 1000000) / max) + " allocation, KeyValuePair"); | |
Console.WriteLine(((double)(s3.Elapsed.TotalMilliseconds * 1000000) / max) + " allocation, Tuple literal"); | |
Console.WriteLine(((double)(s4.Elapsed.TotalMilliseconds * 1000000) / max) + " allocation, Class"); | |
Console.WriteLine(); | |
} | |
static void Argument() | |
{ | |
// Time passing the object as an argument to a function. | |
const int max = 10000000; | |
var a = new Tuple<string, string>("", ""); | |
var b = new KeyValuePair<string, string>("", ""); | |
var c = ("", ""); | |
var d = new UseClass { Item1 = "", Item2 = "" }; | |
X(a); | |
X(b); | |
X(c); | |
X(d); | |
var s1 = Stopwatch.StartNew(); | |
for (var i = 0; i < max; i++) | |
{ | |
X(a); | |
} | |
s1.Stop(); | |
var s2 = Stopwatch.StartNew(); | |
for (var i = 0; i < max; i++) | |
{ | |
X(b); | |
} | |
s2.Stop(); | |
var s3 = Stopwatch.StartNew(); | |
for (var i = 0; i < max; i++) | |
{ | |
X(c); | |
} | |
s3.Stop(); | |
var s4 = Stopwatch.StartNew(); | |
for (var i = 0; i < max; i++) { | |
X(d); | |
} | |
s4.Stop(); | |
Console.WriteLine(((double)(s1.Elapsed.TotalMilliseconds * 1000000) / max) + " argument, Tuple"); | |
Console.WriteLine(((double)(s2.Elapsed.TotalMilliseconds * 1000000) / max) + " argument, KeyValuePair"); | |
Console.WriteLine(((double)(s3.Elapsed.TotalMilliseconds * 1000000) / max) + " argument, Tuple literal"); | |
Console.WriteLine(((double)(s4.Elapsed.TotalMilliseconds * 1000000) / max) + " argument, Class"); | |
Console.WriteLine(); | |
} | |
static void Return() | |
{ | |
// Time returning the object itself. | |
const int max = 10000000; | |
var a = new Tuple<string, string>("", ""); | |
var b = new KeyValuePair<string, string>("", ""); | |
var c = ("", ""); | |
var d = new UseClass { Item1 = "", Item2 = "" }; | |
Y(a); | |
Y(b); | |
Y(c); | |
Y(d); | |
var s1 = Stopwatch.StartNew(); | |
for (var i = 0; i < max; i++) | |
{ | |
Y(a); | |
} | |
s1.Stop(); | |
var s2 = Stopwatch.StartNew(); | |
for (var i = 0; i < max; i++) | |
{ | |
Y(b); | |
} | |
s2.Stop(); | |
var s3 = Stopwatch.StartNew(); | |
for (var i = 0; i < max; i++) | |
{ | |
Y(c); | |
} | |
s3.Stop(); | |
var s4 = Stopwatch.StartNew(); | |
for (var i = 0; i < max; i++) | |
{ | |
Y(d); | |
} | |
s4.Stop(); | |
Console.WriteLine(((double)(s1.Elapsed.TotalMilliseconds * 1000000) / max) + " return, Tuple"); | |
Console.WriteLine(((double)(s2.Elapsed.TotalMilliseconds * 1000000) / max) + " return, KeyValuePair"); | |
Console.WriteLine(((double)(s3.Elapsed.TotalMilliseconds * 1000000) / max) + " return, Tuple literal"); | |
Console.WriteLine(((double)(s4.Elapsed.TotalMilliseconds * 1000000) / max) + " return, Class"); | |
Console.WriteLine(); | |
} | |
static void Load() | |
{ | |
// Time accessing an element. | |
const int max = 10000000; | |
var a = new Tuple<string, string>("cat", "dog"); | |
var b = new KeyValuePair<string, string>("cat", "dog"); | |
var c = ("cat", "dog"); | |
var d = new UseClass { Item1 = "cat", Item2 = "dog" }; | |
var list1 = new List<Tuple<string, string>>(); | |
list1.Add(a); | |
Z(list1); | |
var list2 = new List<KeyValuePair<string, string>>(); | |
list2.Add(b); | |
Z(list2); | |
var list3 = new List<(string, string)>(); | |
list3.Add(c); | |
Z(list3); | |
var list4 = new List<UseClass>(); | |
list4.Add(d); | |
Z(list4); | |
var s1 = Stopwatch.StartNew(); | |
for (var i = 0; i < max; i++) | |
{ | |
Z(list1); | |
} | |
s1.Stop(); | |
var s2 = Stopwatch.StartNew(); | |
for (var i = 0; i < max; i++) | |
{ | |
Z(list2); | |
} | |
s2.Stop(); | |
var s3 = Stopwatch.StartNew(); | |
for (var i = 0; i < max; i++) | |
{ | |
Z(list3); | |
} | |
s3.Stop(); | |
var s4 = Stopwatch.StartNew(); | |
for (var i = 0; i < max; i++) | |
{ | |
Z(list4); | |
} | |
s4.Stop(); | |
Console.WriteLine(((double)(s1.Elapsed.TotalMilliseconds * 1000000) / max) + " load, Tuple"); | |
Console.WriteLine(((double)(s2.Elapsed.TotalMilliseconds * 1000000) / max) + " load, KeyValuePair"); | |
Console.WriteLine(((double)(s3.Elapsed.TotalMilliseconds * 1000000) / max) + " load, Tuple literal"); | |
Console.WriteLine(((double)(s4.Elapsed.TotalMilliseconds * 1000000) / max) + " load, Class"); | |
Console.WriteLine(); | |
} | |
static void ComparisonEquals() { | |
// Time allocating the object. | |
const int max = 1000000; | |
var a = new Tuple<string, string>("", ""); | |
var b = new KeyValuePair<string, string>("", ""); | |
var c = ("", ""); | |
var d = new UseClass {Item1 = "", Item2 = ""}; | |
var tuple = false; | |
var pair = false; | |
var pairl = false; | |
var clas = false; | |
var s1 = Stopwatch.StartNew(); | |
for (var i = 0; i < max; i++) { | |
tuple = Equals(a, new Tuple<string, string>("cat", "dog")); | |
} | |
s1.Stop(); | |
var s2 = Stopwatch.StartNew(); | |
for (var i = 0; i < max; i++) { | |
pair = Equals(b, new KeyValuePair<string, string>("cat", "dog")); | |
} | |
s2.Stop(); | |
var s3 = Stopwatch.StartNew(); | |
for (var i = 0; i < max; i++) { | |
pairl = Equals(c, ("cat", "dog")); | |
} | |
s3.Stop(); | |
var s4 = Stopwatch.StartNew(); | |
for (var i = 0; i < max; i++) { | |
clas = Equals(d, new UseClass {Item1 = "cat", Item2 = "dog"}); | |
} | |
s4.Stop(); | |
Console.WriteLine(((double) (s1.Elapsed.TotalMilliseconds * 1000000) / max) + " comparison Equals(), Tuple ," + tuple); | |
Console.WriteLine(((double) (s2.Elapsed.TotalMilliseconds * 1000000) / max) + " comparison Equals(), KeyValuePair ," + pair); | |
Console.WriteLine(((double) (s3.Elapsed.TotalMilliseconds * 1000000) / max) + " comparison Equals(), Tuple literal ," + pairl); | |
Console.WriteLine(((double) (s4.Elapsed.TotalMilliseconds * 1000000) / max) + " comparison Equals(), Class ,"+ clas); | |
Console.WriteLine(); | |
} | |
static void ComparisonHashCode() { | |
// Time allocating the object. | |
const int max = 1000000; | |
var a = new Tuple<string, string>("", ""); | |
var b = new KeyValuePair<string, string>("", ""); | |
var c = ("", ""); | |
var d = new UseClass {Item1 = "cat", Item2 = "dog"}; | |
var tuple = false; | |
var pair = false; | |
var pairl = false; | |
var clas = false; | |
var s1 = Stopwatch.StartNew(); | |
for (var i = 0; i < max; i++) { | |
tuple = a.GetHashCode() == new Tuple<string, string>("cat", "dog").GetHashCode(); | |
} | |
s1.Stop(); | |
var s2 = Stopwatch.StartNew(); | |
for (var i = 0; i < max; i++) { | |
pair = b.GetHashCode() == new KeyValuePair<string, string>("cat", "dog").GetHashCode(); | |
} | |
s2.Stop(); | |
var s3 = Stopwatch.StartNew(); | |
for (var i = 0; i < max; i++) { | |
pairl = c.GetHashCode() == ("cat", "dog").GetHashCode(); | |
} | |
s3.Stop(); | |
var s4 = Stopwatch.StartNew(); | |
for (var i = 0; i < max; i++) { | |
clas = d.GetHashCode() == new UseClass {Item1 = "cat", Item2 = "dog"}.GetHashCode(); | |
} | |
s4.Stop(); | |
Console.WriteLine(((double) (s1.Elapsed.TotalMilliseconds * 1000000) / max) + " comparison hashcode, Tuple, " + tuple); | |
Console.WriteLine(((double) (s2.Elapsed.TotalMilliseconds * 1000000) / max) + " comparison hashcode, KeyValuePair, " + pair); | |
Console.WriteLine(((double) (s3.Elapsed.TotalMilliseconds * 1000000) / max) + " comparison hashcode, Tuple literal, " + pairl); | |
Console.WriteLine(((double) (s4.Elapsed.TotalMilliseconds * 1000000) / max) + " comparison hashcode, Class, " + clas); | |
Console.WriteLine(); | |
} | |
[MethodImpl(MethodImplOptions.NoInlining)] | |
static void X(Tuple<string, string> a) | |
{ | |
// This and following methods are used in the benchmarks. | |
} | |
[MethodImpl(MethodImplOptions.NoInlining)] | |
static void X(KeyValuePair<string, string> a) | |
{ | |
} | |
[MethodImpl(MethodImplOptions.NoInlining)] | |
static void X((string, string) a) | |
{ | |
} | |
[MethodImpl(MethodImplOptions.NoInlining)] | |
static void X(UseClass a) | |
{ | |
} | |
[MethodImpl(MethodImplOptions.NoInlining)] | |
static Tuple<string, string> Y(Tuple<string, string> a) | |
{ | |
return a; | |
} | |
[MethodImpl(MethodImplOptions.NoInlining)] | |
static KeyValuePair<string, string> Y(KeyValuePair<string, string> a) | |
{ | |
return a; | |
} | |
[MethodImpl(MethodImplOptions.NoInlining)] | |
static (string, string) Y((string, string) a) | |
{ | |
return a; | |
} | |
[MethodImpl(MethodImplOptions.NoInlining)] | |
static UseClass Y(UseClass a) | |
{ | |
return a; | |
} | |
static char Z(List<Tuple<string, string>> list) | |
{ | |
return list[0].Item1[0]; | |
} | |
static char Z(List<KeyValuePair<string, string>> list) | |
{ | |
return list[0].Key[0]; | |
} | |
static char Z(List<(string, string)> list) | |
{ | |
return list[0].Item1[0]; | |
} | |
static char Z(List<UseClass> list) | |
{ | |
return list[0].Item1[0]; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment