1

このコードの実行速度は非常に遅いです:

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 と比較する select ステートメントが問題のようです。この行は、14.8 の %Time で ANTS から 9000 万のヒット数を報告しています。 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();
    }
4

1 に答える 1

4

ANTS は、メイン リストの各項目について、リスト全体を再度検索しているため、 Selectwith 呼び出しが 9000 万回ヒットしたと言っています。string.Compare

最初の 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)));
于 2012-11-27T17:40:11.317 に答える