C# List 根据对象属性去重的四种方法对比
程序员文章站
2022-06-15 09:27:24
测试代码: private void TestDistinct() { Task.Run(() => { //生成测试数据 DateTime dt = DateTime.Now; Random rnd = new Random(); List list = new List
测试代码:
private void testdistinct() { task.run(() => { //生成测试数据 datetime dt = datetime.now; random rnd = new random(); list<mydata> list = new list<mydata>(); int total = 1000000; for (int i = 0; i < total; i++) { mydata info = new mydata(); info.id = rnd.next(1, total * 10).tostring(); info.name = rnd.next(1, total * 10).tostring(); list.add(info); } double d = datetime.now.subtract(dt).totalmilliseconds; //方法一 datetime dt1 = datetime.now; dictionary<string, mydata> result1 = new dictionary<string, mydata>(); foreach (mydata item in list) { mydata temp; if (!result1.trygetvalue(item.name, out temp)) { result1.add(item.name, item); } } list<mydata> r1 = result1.values.tolist(); double d1 = datetime.now.subtract(dt1).totalmilliseconds; //方法二 datetime dt2 = datetime.now; list<mydata> result2 = list.tolookup(item => item.name).todictionary(item => item.key, item => item.first()).values.tolist(); double d2 = datetime.now.subtract(dt2).totalmilliseconds; //方法三 datetime dt3 = datetime.now; list<mydata> result3 = list.distinct(new mycompare()).tolist(); double d3 = datetime.now.subtract(dt3).totalmilliseconds; //方法四 datetime dt4 = datetime.now; list<mydata> result4 = list.groupby(item => item.name).select(item => item.first()).tolist(); double d4 = datetime.now.subtract(dt4).totalmilliseconds; this.begininvoke(new action(() => { textbox1.text = ""; textbox1.text += "生成 " + list.count.tostring("# ####") + " 条测试数据耗时:" + d + "毫秒\r\n\r\n"; textbox1.text += "使用方法一去重耗时:" + d1 + "毫秒\r\n\r\n"; textbox1.text += "使用tolookup和todictionary去重耗时:" + d2 + "毫秒\r\n\r\n"; textbox1.text += "使用distinct去重耗时:" + d3 + "毫秒\r\n\r\n"; textbox1.text += "使用groupby和select去重耗时:" + d4 + "毫秒\r\n\r\n"; textbox1.text += "去重后数量:" + r1.count + "," + result2.count + "," + result3.count + "," + result4.count + "" + "\r\n\r\n"; })); }); }
数据类:
public class mydata { public string id { get; set; } public string name { get; set; } } public class mycompare : iequalitycomparer<mydata> { public bool equals(mydata x, mydata y) { return x.name == y.name; } public int gethashcode(mydata obj) { return obj.name.gethashcode(); } }
测试结果:
结论:
方法一和方法三去重速度差不多,在一个数量级
方法二和方法四去重速度差不多,在一个数量级
方法二和方法四比方法一和方法三大约慢4、5倍左右
方法二和方法四比较方便,一行代码搞定,方法一和方法三代码行数相对较多,方法三要写个mycompare