public void Run() { var models = HashModel.GetHashModels(); TraceHelper.WriteLine("实体相等比较:"); HashSet <HashModel> defaultHashset = new HashSet <HashModel>(models); TraceHelper.WriteLine(">>>增加相同名称的不同实例:"); defaultHashset.Add(new HashModel() { Name = models.First().Name }); TraceHelper.WriteLine(">>>增加已经存在相同实例:"); defaultHashset.Add(models.First()); TraceHelper.WriteLine($">>>defaultHashset:共 {defaultHashset.Count} 个元素\n\t{string.Join("\n\t", defaultHashset.Select(model => model.ToString()))}"); TraceHelper.WriteHr(); TraceHelper.WriteLine("自定义相等比较器:"); HashSet <HashModel> customeHashset = new HashSet <HashModel>(models, new HashModelEqualityComparer()); TraceHelper.WriteLine(">>>增加相同名称的不同实例:"); customeHashset.Add(new HashModel() { Name = models.First().Name }); TraceHelper.WriteLine(">>>增加已经存在相同实例:"); customeHashset.Add(models.First()); TraceHelper.WriteLine($">>>customeHashset:共 {customeHashset.Count} 个元素\n\t{string.Join("\n\t", customeHashset.Select(model => model.ToString()))}"); TraceHelper.WriteHr(); }
/// <summary> /// 默认Hash计算方法 /// </summary> /// <returns></returns> public override int GetHashCode() { int hash = base.GetHashCode(); TraceHelper.WriteLine($"实体Hash计算:{this.Name} => {hash.ToString("X")}"); return(hash); }
/// <summary> /// 默认相等比较算法 /// </summary> /// <param name="other"></param> /// <returns></returns> public bool Equals(HashModel other) { bool equals = ReferenceEquals(this, other); TraceHelper.WriteLine($"实体相等比较:{this.Name} {(equals ? "=" : "!=")} {other.Name}"); return(equals); }
/// <summary> /// 自定义Hash计算方法 /// </summary> /// <param name="obj"></param> /// <returns></returns> public int GetHashCode(HashModel obj) { int hash = obj.Name.GetHashCode(); TraceHelper.WriteLine($"自定义相等比较器Hash计算:{obj.Name} => {hash.ToString("X")}"); return(hash); }
/// <summary> /// 自定义相等比较方法 /// </summary> /// <param name="x"></param> /// <param name="y"></param> /// <returns></returns> public bool Equals(HashModel x, HashModel y) { bool equals = string.Equals(x?.Name, y.Name, StringComparison.OrdinalIgnoreCase); TraceHelper.WriteLine($"自定义相等比较器相等比较:{x.Name} {(equals ? "=" : "!=")} {y.Name}"); return(equals); }
public void Run() { var models = HashModel.GetHashModels(); object lockSeed = new object(); TraceHelper.WriteLine("同步字典:"); Dictionary <string, HashModel> dictionary = new Dictionary <string, HashModel>(); TraceHelper.WriteLine(">>>并发写入:"); Parallel.ForEach( models, (model) => { lock (lockSeed) { dictionary.Add(model.Name, model); } }); TraceHelper.WriteLine($">>>dictionary:共 {dictionary.Count} 个元素\n\t{string.Join("\n\t", dictionary.Select(model => model.ToString()))}"); dictionary.Clear(); TraceHelper.WriteHr(); TraceHelper.WriteLine("并发字典:"); ConcurrentDictionary <string, HashModel> concurrentDictionary = new ConcurrentDictionary <string, HashModel>(); Parallel.ForEach( models, (model) => { concurrentDictionary.TryAdd(model.Name, model); }); TraceHelper.WriteLine($">>>concurrentDictionary:共 {concurrentDictionary.Count} 个元素\n\t{string.Join("\n\t", concurrentDictionary.Select(model => model.ToString()))}"); concurrentDictionary.Clear(); TraceHelper.WriteHr(); TraceHelper.WriteLine(">>>写入字典性能测试:"); Stopwatch stopwatch = new Stopwatch(); int count = 5000000; stopwatch.Start(); Parallel.For(0, count, (index) => { var model = new HashModel(index); lock (lockSeed) { dictionary.Add(model.Name, model); } }); stopwatch.Stop(); TraceHelper.WriteLine($"同步字典并发写入 {count} 次,耗时:{stopwatch.ElapsedMilliseconds.ToString("N")} 毫秒"); stopwatch.Reset(); stopwatch.Start(); Parallel.For(0, count, (index) => { var model = new HashModel(index); concurrentDictionary.TryAdd(model.Name, model); }); stopwatch.Stop(); TraceHelper.WriteLine($"并发字典并发写入 {count} 次,耗时:{stopwatch.ElapsedMilliseconds.ToString("N")} 毫秒"); TraceHelper.WriteHr(); TraceHelper.WriteLine(">>>读取字典性能测试:"); stopwatch.Reset(); stopwatch.Start(); Parallel.For(0, count, (index) => { lock (lockSeed) { dictionary.TryGetValue($"model_{index}", out _); } }); stopwatch.Stop(); TraceHelper.WriteLine($"同步字典并发读取 {count} 次,耗时:{stopwatch.ElapsedMilliseconds.ToString("N")} 毫秒"); stopwatch.Reset(); stopwatch.Start(); Parallel.For(0, count, (index) => { concurrentDictionary.TryGetValue($"model_{index}", out _); }); stopwatch.Stop(); TraceHelper.WriteLine($"并发字典并发读取 {count} 次,耗时:{stopwatch.ElapsedMilliseconds.ToString("N")} 毫秒"); TraceHelper.WriteHr(); }