AUTOMATIC1111

Untitled

Mar 14th, 2019
154
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.54 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using System.Linq;
  5. using System.Text;
  6.  
  7. namespace Test1
  8. {
  9.     class A {
  10.         public string text;
  11.     }
  12.  
  13.     class B : A {
  14.     }
  15.  
  16.     class C : A {
  17.         public override bool Equals(object other)
  18.         {
  19.             return text==(other as C).text;
  20.         }
  21.         public override int GetHashCode()
  22.         {
  23.             return text.GetHashCode();
  24.         }
  25.     }
  26.  
  27.     class Program
  28.     {
  29.         private static Random random = new Random();
  30.         public static string RandomString(int length)
  31.         {
  32.             const string chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
  33.             return new string(Enumerable.Repeat(chars, length)
  34.               .Select(s => s[random.Next(s.Length)]).ToArray());
  35.         }
  36.  
  37.         static void benchmark<T, N>(string label, List<T> listYes, List<N> listNo) where T : A where N: A{
  38.             int ticks = Environment.TickCount;
  39.  
  40.             for (int i = 0; i < 1000; i++)
  41.             {
  42.                 Dictionary<A, int> dict = new Dictionary<A, int>();
  43.  
  44.                 foreach (T t in listYes)
  45.                 {
  46.                     dict.Add(t, 1);
  47.                 }
  48.  
  49.                 foreach (N t in listNo)
  50.                 {
  51.                     if (dict.ContainsKey(t)) throw new Exception("oops!");
  52.                 }
  53.  
  54.                 foreach (T t in listYes)
  55.                 {
  56.                     if (!dict.ContainsKey(t)) throw new Exception("oops!");
  57.                 }
  58.             }
  59.  
  60.             Console.WriteLine(label+" took "+(Environment.TickCount-ticks) + "ms");
  61.         }
  62.  
  63.         static void Main(string[] args)
  64.         {
  65.             Console.WriteLine("B{'xxx'}.HashCode = " + new B { text = "xxx" }.GetHashCode());
  66.             Console.WriteLine("B{'xxx'}.HashCode = " + new B { text = "xxx" }.GetHashCode());
  67.             Console.WriteLine("C{'xxx'}.HashCode = " + new C { text = "xxx" }.GetHashCode());
  68.             Console.WriteLine("C{'xxx'}.HashCode = " + new C { text = "xxx" }.GetHashCode());
  69.  
  70.             List<string> items = new List<string>();
  71.             for (int i = 0; i < 10000; i++) items.Add(RandomString(10));
  72.  
  73.             List<B> itemsB = new List<B>();
  74.             foreach (string item in items) itemsB.Add(new B { text = item });
  75.  
  76.             List<C> itemsC = new List<C>();
  77.             foreach (string item in items) itemsC.Add(new C { text = item });
  78.  
  79.             benchmark("B", itemsB, itemsC);
  80.             benchmark("C", itemsC, itemsB);
  81.         }
  82.     }
  83. }
Advertisement
Add Comment
Please, Sign In to add comment