gpt4 book ai didi

c# - 在 ANTs 分析器信息的帮助下识别性能瓶颈

转载 作者:太空宇宙 更新时间:2023-11-03 21:53:02 25 4
gpt4 key购买 nike

这段代码运行的很慢:

public static class DB
{
readonly static InlineSql sql = new InlineSql();

public static IEnumerable<LabItem> GetLabItemsFromLabLineItems(IEnumerable<LabLineItem> items)
{
/*
* Business Rule :
* The sproc used to retrieve lab line items has no way of "grouping" lab items out of line items.
* To compensate, the sproc orders its results by IDCode, we group everything and use the first of each grouping to represent that ID Code.
* That very same item is also the first item in its Items property.
* */
return items
.GroupBy(c => c.IDCode , c => c, (c, d) => d.First())
.Select(c => new LabItem(c.IDCode, c.OrderGuid, c.Name, c.SignificantDtm, c.Status, c.Description, items.Where(d => string.Compare(d.IDCode, c.IDCode, true) == 0 )));
}
}

特别是我们将 d.IDCode 与 c.IDCode 进行比较的选择语句似乎是问题所在。此行报告来自 ANTS 的命中数为 9000 万,%Time 为 14.8。 items.count大约 9 千。

我知道我的断点没有被击中 9000 万次。这里的命中数是什么意思?

其他有用的代码:

LabItemList<LabLineItem>这就是我们在这里比较的。 LabLineItem.Equals :

    public override bool Equals(object obj)
{
LabLineItem otherItem = obj as LabLineItem;

if (otherItem == null) return false;

return
OrderGuid == otherItem.OrderGuid
&& IDCode == otherItem.IDCode
&& SignificantDtm == otherItem.SignificantDtm
&& ObservationGuid == otherItem.ObservationGuid
&& string.Compare(Value, otherItem.Value, true) == 0;
}
public override int GetHashCode()
{
return
OrderGuid.GetHashCode()
^ IDCode.GetHashCode()
^ SignificantDtm.GetHashCode()
^ ObservationGuid.GetHashCode();
}

最佳答案

ANTS 表示调用 string.CompareSelect 被命中了 9000 万次,因为对于主列表中的每个项目,它都会再次搜索整个列表.

主要的 9000 次迭代中的每一次都会导致 9000 次额外的迭代,因此 string.Compare 必须至少调用 81,000,000 次。

我建议构建一个分组缓存,然后使用它来构建 LabItem

也许是这样的:

var groupedItems = items.GroupBy(c => c.IDCode);

return items.Select(c =>
new LabItem(c.IDCode, c.OrderGuid, c.Name, c.SignificantDtm, c.Status, c.Description,
groupedItems.Where(d => string.Compare(d.Key, c.IDCode, true) == 0 ).SelectMany(group => group)));

关于c# - 在 ANTs 分析器信息的帮助下识别性能瓶颈,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13589405/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com