.NET 的 List 中提供了 ConvertAll
和 Select
两个方法,在开发中实际上应该使用哪一个?
接下来通过基准测试脚本来对比性能。
先编写基准测试脚本:
[MemoryDiagnoser]
public class BenchmarksTerrible
{
private readonly List<Order> _orders;
public BenchmarksTerrible()
{
var random = new Random(420);
_orders = Enumerable.Range(1, 100000).Select(_ => new Order { Status = random.Next().ToString() })
.ToList();;
}
public static OrderBasicInfo ConvertOrder(Order order) => new() { Status = order.Status };
public List<OrderBasicInfo> GetOrderBasicInfos() => _orders.ConvertAll(new Converter<Order, OrderBasicInfo>(ConvertOrder));
[Benchmark]
public List<OrderBasicInfo> ConvertAll()
{
return GetOrderBasicInfos();
}
[Benchmark]
public void Select()
{
var _ = _orders.Select(x => new OrderBasicInfo { Status = x.Status });
}
[Benchmark]
public List<OrderBasicInfo> SelectToList()
{
return _orders.Select(x => new OrderBasicInfo { Status = x.Status })
.ToList();
}
}
测试结果如下:
Method | Mean | Error | StdDev | Gen 0 | Gen 1 | Gen 2 | Allocated |
---|---|---|---|---|---|---|---|
ConvertAll | 4,118,657.86 ns | 77,004.920 ns | 79,078.383 ns | 382.8125 | 375.0000 | 132.8125 | 3200166 B |
Select | 14.70 ns | 0.287 ns | 0.330 ns | 0.0076 | - | - | 72 B |
SelectToList | 4,115,770.49 ns | 68,067.640 ns | 63,670.513 ns | 382.8125 | 375.0000 | 132.8125 | 3200174 B |
然后将代码的 GetOrderBasicInfos
方法进行如下调整,重新测试。
public List<OrderBasicInfo> GetOrderBasicInfos() => _orders.ConvertAll(ConvertOrder);
测试结果如下:
Method | Mean | Error | StdDev | Gen0 | Gen1 | Gen2 | Allocated |
---|---|---|---|---|---|---|---|
ConvertAll | 4,160,022.71 ns | 57,100.202 ns | 50,617.842 ns | 382.8125 | 375.0000 | 132.8125 | 3200166 B |
Select | 14.49 ns | 0.209 ns | 0.174 ns | 0.0076 | - | - | 72 B |
SelectToList | 4,118,527.16 ns | 58,763.369 ns | 49,070.075 ns | 382.8125 | 375.0000 | 132.8125 | 3200174 B |
经过两次测试可以发现,Select
方法的性能最好,因为它的执行时间最短,分配的内存最少,并且几乎不需要垃圾回收。ConvertAll
和 SelectToList
方法的执行时间非常接近,SelectToList
稍好一些,但都比 Select 方法慢得多,且分配的内存较多,垃圾回收次数也较高。
因此,对于开头的问题,我的结论是两个方法的性能差不多,都可以使用。
标签:Status,ConvertAll,new,NET,ns,public,Select From: https://www.cnblogs.com/denglei1024/p/18460395