fork(2) download
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using System.Linq;
  5.  
  6. public class Test
  7. {
  8. public static void Main()
  9. {
  10. TimeSolution(new HashSetSolution());
  11. TimeSolution(new OriginalSolution());
  12. }
  13.  
  14. private static void TimeSolution(ISolution solution)
  15. {
  16. Random random = new Random(7);
  17. var sw = new Stopwatch();
  18. var count = 0;
  19. const int iterations = 1000;
  20. const int arraySize = 5000;
  21. for (int i = 0; i < iterations; i++)
  22. {
  23. var input = GetRandomArray(arraySize, random);
  24. var shuffled = new int[arraySize];
  25. Array.Copy(input, shuffled, arraySize);
  26. Shuffle(input, random);
  27.  
  28. sw.Start();
  29. var areEqual = solution.SetEqual(input, shuffled, null);
  30. sw.Stop();
  31.  
  32. if (areEqual)
  33. {
  34. count++;
  35. }
  36. }
  37.  
  38. Console.WriteLine("{0} / {1}: {2}", count, iterations, sw.Elapsed);
  39. }
  40.  
  41. private static int[] GetRandomArray(int size, Random random)
  42. {
  43. int[] result = new int[size];
  44. for (int i = 0; i < size; i++)
  45. {
  46. result[i] = random.Next();
  47. }
  48.  
  49. return result;
  50. }
  51.  
  52. private static void Shuffle(int[] input, Random random)
  53. {
  54. for (int i = input.Length - 1; i > 0; i--)
  55. {
  56. var j = random.Next(i + 1);
  57. var swap = input[i];
  58. input[i] = input[j];
  59. input[j] = swap;
  60. }
  61. }
  62.  
  63. interface ISolution
  64. {
  65. bool SetEqual<T>(IEnumerable<T> source, IEnumerable<T> other, IEqualityComparer<T> comparer);
  66. }
  67.  
  68. class HashSetSolution : ISolution
  69. {
  70. public bool SetEqual<T>(IEnumerable<T> source, IEnumerable<T> other, IEqualityComparer<T> comparer = null)
  71. {
  72. if (source == null) {
  73. throw new ArgumentNullException("source");
  74. }
  75.  
  76. if (other == null) {
  77. throw new ArgumentNullException("other");
  78. }
  79.  
  80. return new HashSet<T>(source, comparer).SetEquals(other);
  81. }
  82. }
  83.  
  84. class OriginalSolution : ISolution
  85. {
  86. public bool SetEqual<T>(IEnumerable<T> source, IEnumerable<T> other, IEqualityComparer<T> comparer = null)
  87. {
  88. if (source == null) {
  89. throw new ArgumentNullException("source");
  90. }
  91.  
  92. if (other == null) {
  93. throw new ArgumentNullException("other");
  94. }
  95.  
  96. ICollection<T> sourceCollection = source as ICollection<T> ?? source.ToArray();
  97. ICollection<T> otherCollection = other as ICollection<T> ?? other.ToArray();
  98.  
  99. if (sourceCollection.Count != otherCollection.Count) {
  100. return false;
  101. }
  102.  
  103. if (sourceCollection.Except(otherCollection, comparer).Any()) {
  104. return false;
  105. }
  106.  
  107. if (otherCollection.Except(sourceCollection, comparer).Any()) {
  108. return false;
  109. }
  110.  
  111. return true;
  112. }
  113. }
  114. }
Success #stdin #stdout 4.55s 35240KB
stdin
Standard input is empty
stdout
1000 / 1000: 00:00:01.5158242
1000 / 1000: 00:00:02.1985785