fork download
  1. using System;
  2. using System.Linq;
  3. using System.Collections.Generic;
  4.  
  5. public class Test
  6. {
  7. public static void Main()
  8. {
  9. var numbers = new List<int>();
  10. numbers.Add(1);
  11. for (int i = 1; i <= 3000000; i++)
  12. {
  13. numbers.Add(i);
  14. }
  15. var sw = new System.Diagnostics.Stopwatch();
  16. var memBefore = GC.GetTotalMemory(true);
  17. sw.Start();
  18. var set = new HashSet<int>(numbers);
  19. bool allUnique = numbers.Count == set.Count;
  20. sw.Stop();
  21. String constructorMeasurement = string.Format(
  22. "Set constructor memory consumption: {0} MB, duration: {1}"
  23. , (GC.GetTotalMemory(true) - memBefore) / (1024 * 1024)
  24. , sw.Elapsed);
  25.  
  26. set = new HashSet<int>();
  27. memBefore = GC.GetTotalMemory(true);
  28. sw = new System.Diagnostics.Stopwatch();
  29. sw.Start();
  30. allUnique = numbers.All(n => set.Add(n));
  31. sw.Stop();
  32. String enumerableAllMeasurement = string.Format(
  33. "Enumerable.All memory consumption: {0} MB, duration: {1}"
  34. , (GC.GetTotalMemory(true) - memBefore) / (1024 * 1024)
  35. , sw.Elapsed);
  36.  
  37. Console.WriteLine( constructorMeasurement );
  38. Console.WriteLine( enumerableAllMeasurement);
  39. }
  40. }
Success #stdin #stdout 0.4s 145536KB
stdin
Standard input is empty
stdout
Set constructor memory consumption: 50 MB, duration: 00:00:00.2962615
Enumerable.All memory consumption: 0 MB, duration: 00:00:00.0004254