c# – 将列表细分为多个列表时System.Linq的性能问题

c# – 将列表细分为多个列表时System.Linq的性能问题,第1张

概述我写了一个方法,使用System. Linq将项目列表细分为多个列表. 当我为50000个简单整数运行此方法时,它需要大约59.862秒. Stopwatch watchresult0 = new Stopwatch();watchresult0.Start();var result0 = SubDivideListLinq(Enumerable.Range(0, 50000), 100).T 我写了一个方法,使用System. Linq将项目列表细分为多个列表.
当我为50000个简单整数运行此方法时,它需要大约59.862秒.

Stopwatch watchresult0 = new Stopwatch();watchresult0.Start();var result0 = SubdivIDeListlinq(Enumerable.Range(0,50000),100).ToList();watchresult0.Stop();long elapsedresult0 = watchresult0.ElapsedMilliseconds;

所以我试着提升它,并用一个简单的循环来编写它,迭代我列表中的每个项目,它只需要4毫秒:

Stopwatch watchresult1 = new Stopwatch();watchresult1.Start();var result1 = SubdivIDeList(Enumerable.Range(0,100).ToList();watchresult1.Stop();long elapsedresult1 = watchresult1.ElapsedMilliseconds;

这是我使用linq的SubdivIDe方法:

private static IEnumerable<List<T>> SubdivIDeListlinq<T>(IEnumerable<T> enumerable,int count){    while (enumerable.Any())    {        yIEld return enumerable.Take(count).ToList();        enumerable = enumerable.Skip(count);    }}

这是我的SubdivIDe方法,每个项目都有foreach循环:

private static IEnumerable<List<T>> SubdivIDeList<T>(IEnumerable<T> enumerable,int count){    List<T> allitems = enumerable.ToList();    List<T> items = new List<T>(count);    foreach (T item in allitems)    {        items.Add(item);        if (items.Count != count) continue;        yIEld return items;        items = new List<T>(count);    }    if (items.Any())        yIEld return items;}

你有任何想法,为什么我自己的实现比用linq划分要快得多?或者我做错了什么?

并且:正如您所看到的,我知道如何拆分列表,因此这不是相关问题的重复.我想知道linq和我的实现之间的性能.不是如何拆分列表

解决方法 如果您追求可读性和性能您可能希望使用此算法.在速度方面,这个非常接近你的非linq版本.同时它更具可读性.

private static IEnumerable<List<T>> SubdivIDeListlinq<T>(IEnumerable<T> enumerable,int count){    int index = 0;    return enumerable.GroupBy(l => index++/count).Select(l => l.ToList());}

它的替代方案:

private static IEnumerable<List<T>> SubdivIDeListlinq<T>(IEnumerable<T> enumerable,int count){    int index = 0;    return from l in enumerable        group l by index++/count        into l select l.ToList();}

另一种选择:

private static IEnumerable<List<T>> SubdivIDeListlinq<T>(IEnumerable<T> enumerable,int count){    int index = 0;    return enumerable.GroupBy(l => index++/count,item => item,(key,result) => result.ToList());}

在我的电脑中,我得到linq 0.006秒对非linq 0.002秒,这是完全公平的并且可以使用linq.

作为建议,不要用微优化代码折磨自己.显然没有人会感觉到几毫秒的差异,所以写一个代码,以后你和其他人可以轻松理解.

总结

以上是内存溢出为你收集整理的c# – 将列表细分为多个列表时System.Linq的性能问题全部内容,希望文章能够帮你解决c# – 将列表细分为多个列表时System.Linq的性能问题所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/langs/1232615.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-06
下一篇 2022-06-06

发表评论

登录后才能评论

评论列表(0条)

保存