- r - 以节省内存的方式增长 data.frame
- ruby-on-rails - ruby/ruby on rails 内存泄漏检测
- android - 无法解析导入android.support.v7.app
- UNIX 域套接字与共享内存(映射文件)
下面给出了查找IEnumerable
source.Aggregate(0, (result, element) => result + element);
需要 3 毫秒
source.Sum(c => c);
需要 12 毫秒
source.Sum();
需要 1 毫秒
我想知道为什么第二个实现比第一个实现贵四倍。不应该和第三种实现一样吗。
最佳答案
注意:我的电脑运行的是 .Net 4.5 RC,所以我的结果可能会受到影响。
测量一个方法只执行一次所花费的时间通常不是很有用。它很容易被 JIT 编译之类的东西所控制,这在实际代码中并不是真正的瓶颈。因此,我测量了每个方法执行 100 次(在没有附加调试器的 Release模式下)。我的结果是:
聚合()
:9 毫秒Sum(lambda)
:12 毫秒Sum()
:6 毫秒Sum()
是最快的这一事实不足为奇:它包含一个没有任何委托(delegate)调用的简单循环,速度非常快。 Sum(lambda)
和 Aggregate()
之间的差异并不像您所测量的那么明显,但它仍然存在。可能是什么原因呢?让我们看看这两种方法的反编译代码:
public static TAccumulate Aggregate<TSource, TAccumulate>(this IEnumerable<TSource> source, TAccumulate seed, Func<TAccumulate, TSource, TAccumulate> func)
{
if (source == null)
throw Error.ArgumentNull("source");
if (func == null)
throw Error.ArgumentNull("func");
TAccumulate local = seed;
foreach (TSource local2 in source)
local = func(local, local2);
return local;
}
public static int Sum<TSource>(this IEnumerable<TSource> source, Func<TSource, int> selector)
{
return source.Select<TSource, int>(selector).Sum();
}
如您所见,Aggregate()
使用循环,但 Sum(lambda)
使用 Select()
,后者又使用一个循环迭代器。使用迭代器意味着会产生一些开销:创建迭代器对象和(可能更重要的是)为每个项目再调用一次方法。
让我们通过编写我们自己的 Sum(lambda)
两次来验证使用 Select()
实际上是原因,一次使用 Select()
,其行为应与框架中的 Sum(lambda)
相同,并且一次不使用 Select()
:
public static int SlowSum<T>(this IEnumerable<T> source, Func<T, int> selector)
{
return source.Select(selector).Sum();
}
public static int FastSum<T>(this IEnumerable<T> source, Func<T, int> selector)
{
if (source == null)
throw new ArgumentNullException("source");
if (selector == null)
throw new ArgumentNullException("selector");
int num = 0;
foreach (T item in source)
num += selector(item);
return num;
}
我的测量证实了我的想法:
SlowSum(lambda)
:12 毫秒FastSum(lambda)
:9 毫秒关于c# - LINQ 中的聚合与求和性能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11030109/
我正在尝试在 OCaml 中创建一个函数,该函数在数学中执行求和函数。 我试过这个: sum n m f = if n = 0 then 0 else if n > m then f
我正在尝试找到一个可以帮助我解决问题的公式。 这个公式应该对每个靠近(总是在左边)具有相同名称的单元格的单元格求和(或工作)。如下所示: 将每个大写字母视为 “食谱”并且每个小写字母为 “成分” .在
让它成为以下 python pandas DataFrame,其中每一行代表一个人在酒店的住宿。 | entry_date | exit_date | days | other_columns
我有显示客户来电的数据。我有客户号码、电话号码(1 个客户可以有多个)、每个语音调用的日期记录以及调用持续时间的列。表看起来如下示例。 CusID | PhoneNum | Date
让它成为以下 python pandas DataFrame,其中每一行代表一个人在酒店的住宿。 | entry_date | exit_date | days | other_columns
我得到了两列数据; 答: 2013年12月31日 2013年12月30日 2013年12月29日 2013年12月28日 2013年12月27日 2012年12月26日 B: 10 10 10 10
我对 double 格式的精度有疑问。 示例: double K=0, L=0, M=0; scanf("%lf %lf %lf", &K, &L, &M); if((K+L) 我的测试输入: K
我有以下数组: int[,] myArray1 = new int[2, 3] { { 1, 2, 3 }, { 4, 6, 8 } }; int[,] myArray2 = new int[2, 3
我需要有关报告查询的帮助。我在该方案的底部有一个发票表,需要一种方法来获取总计费金额,同时在此数据库方案中的较高点进行条件过滤。我需要加入其他表,这会导致 SUM 函数返回不需要的结果。 这是我正在使
我有一个使用innodb作为存储引擎的MySQL数据库,并且我有许多采用基本形式的查询: SELECT bd.billing, SUM(CASE WHEN tc.transaction_class
尝试创建一个查询来给出总胜、平和负。我有以下查询 SELECT CASE WHEN m.home_team = '192' AND m.home_full_time_score
我正在尝试生成一份报告,显示排名靠前的推荐人以及他们推荐的人产生了多少收入。 这是我的表格的缩写版本: Users Table ------------------ id referral_user_
我有以下查询,并得到了预期的结果: SELECT IF (a1>b1,'1','0') AS a1r, IF (a2>b2,'1','0') AS a2r,
我尝试了几种不同的解决方案,但都没有成功。我给出的表格是一个示例,其设计和功能与我实际使用的表格类似: PK | Color | Count -------------------
我正在尝试构建一个查询来检查我的库存。 SELECT COUNT(*) AS item_count, reseller_id, sum(sold) as sold_count, sum(refunde
我试图解决一个看起来像下面编写的代码的问题,但由于缺乏知识和阅读 sqlalchemy 文档,我还没有真正找到解决问题的方法。 目标: 如果 year_column 中的年份相同,则获取 sales_
我有一个包含一周中多天的表格。一周中的每一天都有独特的属性,例如冰淇淋是否在这一天成功送达: ID DAY_WEEK ICE_CREAM 1 Monday
首先,我有一个名为store_00的表 id | ref | item | qty | cost | sell 1 22 x1 5 10 15 2 22
我正在编写一个程序,计算每个数字的总和,直到 1000。例如,1+2+3+4+5....+100。首先,我将求和作业分配给 10 个处理器:处理器 0 得到 1-100,处理器 1 得到 101-20
我想在一个循环中一次对多个属性求和: class Some(object): def __init__(self, acounter, bcounter): self.acou
我是一名优秀的程序员,十分优秀!