using System; using System.Collections.Generic; using System.Diagnostics; using System.Runtime.CompilerServices; class Program { static void Main() { Allocation(); Argument(); Return(); Load(); Console.Read(); } static void Allocation() { const int max = 1000000; var a = new Tuple("", ""); var b = new KeyValuePair("", ""); var s1 = Stopwatch.StartNew(); for (int i = 0; i < max; i++) { var tuple = new Tuple("cat", "dog"); } s1.Stop(); var s2 = Stopwatch.StartNew(); for (int i = 0; i < max; i++) { var pair = new KeyValuePair("cat", "dog"); } s2.Stop(); Console.WriteLine(((double)(s1.Elapsed.TotalMilliseconds * 1000000) / max).ToString("0.00 ns")); Console.WriteLine(((double)(s2.Elapsed.TotalMilliseconds * 1000000) / max).ToString("0.00 ns")); Console.WriteLine(); } static void Argument() { const int max = 10000000; var a = new Tuple< string, string>("", ""); var b = new KeyValuePair("", ""); X(a); X(b); var s1 = Stopwatch.StartNew(); for (int i = 0; i < max; i++) { X(a); } s1.Stop(); var s2 = Stopwatch.StartNew(); for (int i = 0; i < max; i++) { X(b); } s2.Stop(); Console.WriteLine(((double)(s1.Elapsed.TotalMilliseconds * 1000000) / max).ToString("0.00 ns")); Console.WriteLine(((double)(s2.Elapsed.TotalMilliseconds * 1000000) / max).ToString("0.00 ns")); Console.WriteLine(); } static void Return() { const int max = 10000000; var a = new Tuple("", ""); var b = new KeyValuePair("", ""); Y(a); Y(b); var s1 = Stopwatch.StartNew(); for (int i = 0; i < max; i++) { Y(a); } s1.Stop(); var s2 = Stopwatch.StartNew(); for (int i = 0; i < max; i++) { Y(b); } s2.Stop(); Console.WriteLine(((double)(s1.Elapsed.TotalMilliseconds * 1000000) / max).ToString("0.00 ns")); Console.WriteLine(((double)(s2.Elapsed.TotalMilliseconds * 1000000) / max).ToString("0.00 ns")); Console.WriteLine(); } static void Load() { const int max = 10000000; var a = new Tuple("cat", "dog"); var b = new KeyValuePair("cat", "dog"); List> list1 = new List>(); list1.Add(a); Z(list1); List> list2 = new List>(); list2.Add(b); Z(list2); var s1 = Stopwatch.StartNew(); for (int i = 0; i < max; i++) { Z(list1); } s1.Stop(); var s2 = Stopwatch.StartNew(); for (int i = 0; i < max; i++) { Z(list2); } s2.Stop(); Console.WriteLine(((double)(s1.Elapsed.TotalMilliseconds * 1000000) / max).ToString("0.00 ns")); Console.WriteLine(((double)(s2.Elapsed.TotalMilliseconds * 1000000) / max).ToString("0.00 ns")); Console.WriteLine(); } [MethodImpl(MethodImplOptions.NoInlining)] static void X(Tuple a) { } [MethodImpl(MethodImplOptions.NoInlining)] static void X(KeyValuePair a) { } [MethodImpl(MethodImplOptions.NoInlining)] static Tuple Y(Tuple a) { return a; } [MethodImpl(MethodImplOptions.NoInlining)] static KeyValuePair Y(KeyValuePair a) { return a; } static char Z(List> list) { return list[0].Item1[0]; } static char Z(List> list) { return list[0].Key[0]; } }