C#集合数据去重的5种方式及其性能对比测试分析
C#集合数据去重的5种方式及其性能对比测试分析
今天我们一起来讨论一下关于C#集合数据去重的5种方式并且使用BenchmarkDotet
对这5种方式进行性能基准对比测试分析,每种方法都有其特点和适用场景,我们可以根据具体需求和执行效率选择一种进行使用。
BenchmarkDotet是一个基于.ET开源、功能全面、易于使用的性能基准测试框架,它为.ET开发者提供了强大的性能评估和优化能力。通过自动化测试、多平台支持、高级统计分析和自定义配置等特性,BenchmarkDotet帮助开发者更好地理解和优化软件系统的性能表现。
- 使用详细介绍: 使用 BenchmarkDotet 对 .ET 代码进行性能基准测试
C# 中的 HashSet 是一种集合类型,它确保其中的元素是唯一的,不允许重复值的存在。当你尝试向 HashSet 中添加一个重复的元素时,HashSet 会忽略重复的值,而不会引发错误。这使得 HashSet 成为一个非常方便的数据结构,用于存储一组唯一的元素,并且在需要时可以高效地进行查、插入和删除操作,注意HashSet中的元素是无序的。
代码语言:javascript代码运行次数:0运行复制 /// <summary>
/// 使用HashSet去重
/// TODO:HashSet是一个集合类,它的特点是不允许重复元素,可以方便地实现去重功能。
/// </summary>
public static void HashSetDuplicate()
{
var dataSource = new List<int>() { 1, 2, , 2, 5, 88, 99, 99, 100, 88, 0, 50, 15, 100, 99, 99, 2, };
HashSet<int> uniqueData = new HashSet<int>(dataSource);
Cole.WriteLine(string.Join(", ", uniqueData));
}
/// <summary>
/// 直接循环遍历去重
/// </summary>
public static void LoopTraversalDuplicate()
{
var dataSource = new List<int>() { 1, 2, , 2, 5, 88, 99, 99, 100, 88, 0, 50, 15, 100, 99, 99, 2, };
var uniqueData = new List<int>();
foreach (var item in dataSource)
{
//if (!uniqueData.Any(x => x == item))
//if (!uniqueData.Exists(x => x == item))
if (!uniqueData.Contains(item))
{
uniqueData.Add(item);
}
}
Cole.WriteLine(string.Join(", ", uniqueData));
}
Linq中的Distinct()方法用于从集合中筛选出不重复的元素。Distinct()方法基于元素的相等性来进行筛选,并返回一个包含不重复元素的新序列。底层实现还是使用到了HashSet。
代码语言:javascript代码运行次数:0运行复制 /// <summary>
/// 使用Linq的Distinct()方法去重
/// </summary>
public static void DistinctDuplicate()
{
var dataSource = new List<int>() { 1, 2, , 2, 5, 88, 99, 99, 100, 88, 0, 50, 15, 100, 99, 99, 2, };
var uniqueData = dataSource.Distinct().ToList();
Cole.WriteLine(string.Join(", ", uniqueData));
}
GroupBy()方法将原始集合中的元素进行分组,根据指定的键或条件进行分组。每个分组都会有一个唯一的键,通过将原始集合分组并选择每个分组中的第一个元素,实现了去重的效果。
代码语言:javascript代码运行次数:0运行复制 /// <summary>
/// 使用Linq的GroupBy()方法去重
/// </summary>
public static void GroupByDuplicate()
{
var dataSource = new List<int>() { 1, 2, , 2, 5, 88, 99, 99, 100, 88, 0, 50, 15, 100, 99, 99, 2, };
//GroupBy()方法将原始集合中的元素进行分组,根据指定的键或条件进行分组。每个分组都会有一个唯一的键,通过将原始集合分组并选择每个分组中的第一个元素,实现了去重的效果。
var uniqueData = dataSource.GroupBy(item => item).Select(group => group.First()).ToList();
Cole.WriteLine(string.Join(", ", uniqueData));
}
/// <summary>
/// 使用自定义的比较器和循环遍历
/// </summary>
public static void CustomEqualityComparerDuplicate()
{
var dataSource = new List<int>() { 1, 2, , 2, 5, 88, 99, 99, 100, 88, 0, 50, 15, 100, 99, 99, 2, };
var uniqueData = new List<int>();
foreach (var item in dataSource)
{
if (!uniqueData.Contains(item, new CustomEqualityComparer()))
{
uniqueData.Add(item);
}
}
Cole.WriteLine(string.Join(", ", uniqueData));
}
/// <summary>
/// 自定义的比较器
/// </summary>
public class CustomEqualityComparer : IEqualityComparer<int>
{
public bool Equals(int x, int y)
{
return x == y;
}
public int GetHashCode(int obj)
{
return obj.GetHashCode();
}
}
接下来我们使用BenchmarkDotet
对这5种集合去重的方式进行性能基准对比测试分析。
测试代码
代码语言:javascript代码运行次数:0运行复制using BenchmarkDotet.Attributes;
namespace BenchmarkDotetExercise
{
[MemoryDiagnoser]//记录内存分配情况
public class DataSetDeduplicationBenchmark
{
private List<int> dataSource;
public DataSetDeduplicationBenchmark()
{
// 生成大量重复数据
dataSource = Enumerable.Repeat(Enumerable.Range(1, 100), 10000).SelectMany(x => x).ToList();
}
/// <summary>
/// 使用HashSet去重
/// TODO:HashSet是一个集合类,它的特点是不允许重复元素,可以方便地实现去重功能。
/// </summary>
[Benchmark]
public void HashSetDuplicate()
{
HashSet<int> uniqueData = new HashSet<int>(dataSource);
}
/// <summary>
/// 直接循环遍历去重
/// </summary>
[Benchmark]
public void LoopTraversalDuplicate()
{
var uniqueData = new List<int>();
foreach (var item in dataSource)
{
//if (!uniqueData.Any(x => x == item))
//if (!uniqueData.Exists(x => x == item))
if (!uniqueData.Contains(item))
{
uniqueData.Add(item);
}
}
}
/// <summary>
/// 使用Linq的Distinct()方法去重
/// </summary>
[Benchmark]
public void DistinctDuplicate()
{
var uniqueData = dataSource.Distinct().ToList();
}
/// <summary>
/// 使用Linq的GroupBy()方法去重
/// </summary>
[Benchmark]
public void GroupByDuplicate()
{
//GroupBy()方法将原始集合中的元素进行分组,根据指定的键或条件进行分组。每个分组都会有一个唯一的键,通过将原始集合分组并选择每个分组中的第一个元素,实现了去重的效果。
var uniqueData = dataSource.GroupBy(item => item).Select(group => group.First()).ToList();
}
/// <summary>
/// 使用自定义的比较器和循环遍历
/// </summary>
[Benchmark]
public void CustomEqualityComparerDuplicate()
{
var uniqueData = new List<int>();
foreach (var item in dataSource)
{
if (!uniqueData.Contains(item, new CustomEqualityComparer()))
{
uniqueData.Add(item);
}
}
}
/// <summary>
/// 自定义的比较器
/// </summary>
public class CustomEqualityComparer : IEqualityComparer<int>
{
public bool Equals(int x, int y)
{
return x == y;
}
public int GetHashCode(int obj)
{
return obj.GetHashCode();
}
}
}
}
分析生成的报告
Method | Mean | Error | StdDev | Gen0 | Gen1 | Gen2 | Allocated |
---|---|---|---|---|---|---|---|
HashSetDuplicate | 7.04 ms | 0.0546 ms | 0.0511 ms | 4.7500 | 4.7500 | 4.7500 | 18169.6 KB |
LoopTraversalDuplicate | 7.85 ms | 0.009 ms | 0.0274 ms | - | - | - | 1.16 KB |
DistinctDuplicate | 7.04 ms | 0.0497 ms | 0.0465 ms | 4.7500 | 4.7500 | 4.7500 | 18170.1 KB |
GroupByDuplicate | 12.685 ms | 0.1025 ms | 0.0958 ms | 2265.6250 | 1781.2500 | 515.6250 | 1284.65 KB |
CustomEqualityComparerDuplicate | 25.608 ms | 0.1826 ms | 0.1708 ms | 812.5000 | - | - | 248.68 KB |
说明:
- Mean: 所有测量值的算术平均值。
- Error: 99.9% 置信区间的一半。
- StdDev: 所有测量值的标准差。
- Gen0: 第 0 代 GC 每 1000 次操作收集一次。
- Gen1: 第 1 代 GC 每 1000 次操作收集一次。
- Gen2: 第 2 代 GC 每 1000 次操作收集一次。
- Allocated: 每次操作分配的内存(仅托管内存,包含所有内容,1KB = 1024B)。
- 1 ms: 1 毫秒(0.001 秒)
#感谢您对电脑配置推荐网 - 最新i3 i5 i7组装电脑配置单推荐报价格的认可,转载请说明来源于"电脑配置推荐网 - 最新i3 i5 i7组装电脑配置单推荐报价格
留言与评论(共有 10 条评论) |
本站网友 火病 | 17分钟前 发表 |
可以方便地实现去重功能 | |
本站网友 fiddler4 | 4分钟前 发表 |
}; var uniqueData = new List<int>(); foreach (var item in dataSource) { //if (!uniqueData.Any(x => x == item)) //if (!uniqueData.Exists(x => x == item)) if (!uniqueData.Contains(item)) { uniqueData.Add(item); } } Cole.WriteLine(string.Join(" | |
本站网友 首创新悦都 | 16分钟前 发表 |
Gen0 | |
本站网友 彭彪 | 11分钟前 发表 |
}; HashSet<int> uniqueData = new HashSet<int>(dataSource); Cole.WriteLine(string.Join(" | |
本站网友 geng | 20分钟前 发表 |
第 0 代 GC 每 1000 次操作收集一次 | |
本站网友 蜂花粉的吃法 | 5分钟前 发表 |
所有测量值的算术平均值 | |
本站网友 安神补脑 | 25分钟前 发表 |
它为.ET开发者提供了强大的性能评估和优化能力 | |
本站网友 塑化剂危害 | 15分钟前 发表 |
实现了去重的效果 | |
本站网友 北京新天地 | 29分钟前 发表 |
0 |