using System;
using System.Linq;
using System.Collections.Generic;
public class Test
{
public static void Main()
{
var numbers = new List<int>();
numbers.Add(1);
for (int i = 1; i <= 3000000; i++)
{
numbers.Add(i);
}
var sw = new System.Diagnostics.Stopwatch();
var memBefore = GC.GetTotalMemory(true);
sw.Start();
var set = new HashSet<int>(numbers);
bool allUnique = numbers.Count == set.Count;
sw.Stop();
String constructorMeasurement = string.Format(
"Set constructor memory consumption: {0} MB, duration: {1}"
, (GC.GetTotalMemory(true) - memBefore) / (1024 * 1024)
, sw.Elapsed);
set = new HashSet<int>();
memBefore = GC.GetTotalMemory(true);
sw = new System.Diagnostics.Stopwatch();
sw.Start();
allUnique = numbers.All(n => set.Add(n));
sw.Stop();
String enumerableAllMeasurement = string.Format(
"Enumerable.All memory consumption: {0} MB, duration: {1}"
, (GC.GetTotalMemory(true) - memBefore) / (1024 * 1024)
, sw.Elapsed);
Console.WriteLine( constructorMeasurement );
Console.WriteLine( enumerableAllMeasurement);
}
}
dXNpbmcgU3lzdGVtOwp1c2luZyBTeXN0ZW0uTGlucTsKdXNpbmcgU3lzdGVtLkNvbGxlY3Rpb25zLkdlbmVyaWM7CgpwdWJsaWMgY2xhc3MgVGVzdAp7CglwdWJsaWMgc3RhdGljIHZvaWQgTWFpbigpCgl7CiAgICAgICAgICAgIHZhciBudW1iZXJzID0gbmV3IExpc3Q8aW50PigpOyAKICAgICAgICAgICAgbnVtYmVycy5BZGQoMSk7CiAgICAgICAgICAgIGZvciAoaW50IGkgPSAxOyBpIDw9IDMwMDAwMDA7IGkrKykKICAgICAgICAgICAgewogICAgICAgICAgICAgICAgbnVtYmVycy5BZGQoaSk7CiAgICAgICAgICAgIH0KCSAgICB2YXIgc3cgPSBuZXcgU3lzdGVtLkRpYWdub3N0aWNzLlN0b3B3YXRjaCgpOwogICAgICAgICAgICB2YXIgbWVtQmVmb3JlID0gR0MuR2V0VG90YWxNZW1vcnkodHJ1ZSk7CiAgICAgICAgICAgIHN3LlN0YXJ0KCk7CiAgICAgICAgICAgIHZhciBzZXQgPSBuZXcgSGFzaFNldDxpbnQ+KG51bWJlcnMpOwogICAgICAgICAgICBib29sIGFsbFVuaXF1ZSA9IG51bWJlcnMuQ291bnQgPT0gc2V0LkNvdW50OwogICAgICAgICAgICBzdy5TdG9wKCk7CiAgICAgICAgICAgIFN0cmluZyBjb25zdHJ1Y3Rvck1lYXN1cmVtZW50ID0gc3RyaW5nLkZvcm1hdCgKICAgICAgICAgICAgICAgICJTZXQgY29uc3RydWN0b3IgbWVtb3J5IGNvbnN1bXB0aW9uOiB7MH0gTUIsIGR1cmF0aW9uOiB7MX0iCiAgICAgICAgICAgICAgICAsIChHQy5HZXRUb3RhbE1lbW9yeSh0cnVlKSAtIG1lbUJlZm9yZSkgLyAoMTAyNCAqIDEwMjQpCiAgICAgICAgICAgICAgICAsIHN3LkVsYXBzZWQpOwoKICAgICAgICAgICAgc2V0ID0gbmV3IEhhc2hTZXQ8aW50PigpOwogICAgICAgICAgICBtZW1CZWZvcmUgPSBHQy5HZXRUb3RhbE1lbW9yeSh0cnVlKTsKICAgICAgICAgICAgc3cgPSBuZXcgU3lzdGVtLkRpYWdub3N0aWNzLlN0b3B3YXRjaCgpOwogICAgICAgICAgICBzdy5TdGFydCgpOwogICAgICAgICAgICBhbGxVbmlxdWUgPSBudW1iZXJzLkFsbChuID0+IHNldC5BZGQobikpOwogICAgICAgICAgICBzdy5TdG9wKCk7CiAgICAgICAgICAgIFN0cmluZyBlbnVtZXJhYmxlQWxsTWVhc3VyZW1lbnQgPSBzdHJpbmcuRm9ybWF0KAogICAgICAgICAgICAgICAgIkVudW1lcmFibGUuQWxsIG1lbW9yeSBjb25zdW1wdGlvbjogezB9IE1CLCBkdXJhdGlvbjogezF9IgogICAgICAgICAgICAgICAgLCAoR0MuR2V0VG90YWxNZW1vcnkodHJ1ZSkgLSBtZW1CZWZvcmUpIC8gKDEwMjQgKiAxMDI0KQogICAgICAgICAgICAgICAgLCBzdy5FbGFwc2VkKTsKCgkgICAgQ29uc29sZS5Xcml0ZUxpbmUoIGNvbnN0cnVjdG9yTWVhc3VyZW1lbnQgKTsKCSAgICBDb25zb2xlLldyaXRlTGluZSggZW51bWVyYWJsZUFsbE1lYXN1cmVtZW50KTsKCX0KfQ==