fork download
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Diagnostics;
  5.  
  6. public class Demo
  7. {
  8. public static void Main()
  9. {
  10. int tries = 100;
  11. int count = 50;
  12. int size = 1000;
  13. Random rnd = new Random();
  14. List<int>[] list;
  15. Stopwatch sw;
  16.  
  17. sw = new Stopwatch();
  18. for(int x=0; x<tries; x++)
  19. {
  20. list = new List<int>[count];
  21. for(int y=0; y<count; y++)
  22. {
  23. list[y] = new List<int>();
  24. for(int z=0; z<size; z++)
  25. {
  26. int n = rnd.Next();
  27. list[y].Add(n);
  28. }
  29. if((y % 5) == 0 && y > 0)
  30. { // make repeated lists for the uniqueness check
  31. list[y-1] = new List<int>(list[y]);
  32. list[y-1].Reverse();
  33. }
  34. }
  35. sw.Start();
  36. Test1(list);
  37. sw.Stop();
  38. }
  39. Console.WriteLine( sw.Elapsed.ToString() );
  40.  
  41. sw = new Stopwatch();
  42. for(int x=0; x<tries; x++)
  43. {
  44. list = new List<int>[count];
  45. for(int y=0; y<count; y++)
  46. {
  47. list[y] = new List<int>();
  48. for(int z=0; z<size; z++)
  49. {
  50. int n = rnd.Next();
  51. list[y].Add(n);
  52. }
  53. if((y % 5) == 0 && y > 0)
  54. { // make repeated lists for the uniqueness check
  55. list[y-1] = new List<int>(list[y]);
  56. list[y-1].Reverse();
  57. }
  58. }
  59. sw.Start();
  60. Test2(list);
  61. sw.Stop();
  62. }
  63. Console.WriteLine( sw.Elapsed.ToString() );
  64. }
  65. public static List<List<int>> Test1(List<int>[] lists)
  66. {
  67. var result = new List<List<int>>();
  68. foreach(var list in lists)
  69. {
  70. list.Sort();
  71. if(!result.Any(elm => elm.SequenceEqual(list)))
  72. result.Add(list);
  73. }
  74. return result;
  75. }
  76. public static List<HashSet<int>> Test2(List<int>[] lists)
  77. {
  78. var result = new List<HashSet<int>>();
  79. foreach(var list in lists)
  80. {
  81. result.Add(new HashSet<int>(list));
  82. }
  83. result = result.Distinct(HashSet<int>.CreateSetComparer()).ToList();
  84. return result;
  85. }
  86. }
Success #stdin #stdout 2.63s 40656KB
stdin
Standard input is empty
stdout
00:00:01.0761675
00:00:00.9982699