Improve performance of TryGetValue

214 views Asked by At

I am creating an Excel file using Open XML SDK. In this process, I have a scenario like below.

I need to add data into a Dictionary<uint, string> if key is not exists. For that I am using below code.

var dataLines = sheetData.Elements<Row>().ToList();

for (int i = 0; i < dataLines.Count; i++)
{
    var x = dataLines[i];
    if (!dataDictionary.TryGetValue(x.RowIndex.Value, out var res)) // 700 seconds, 1,279,999,998 Hit counts
    {
        dataDictionary.Add(x.RowIndex.Value, x.OuterXml);
    }
}

When I am trying to create an Excel sheet which has rows around 90,000 - 92,000, the line with the IF condition in above code consume 700 seconds to complete. (checked with a performance profiler, also this line has 1,279,999,998 Hit counts).

How could I reduce the time the line with the IF condition in above code consumes?

Is there any better way to achive this with less time?

1

There are 1 answers

1
NightOwl888 On

If the if statement is slow, one option you have is to eliminate it entirely and use the indexer of the dictionary to set the value. This means that the "last match will win". If you want the "first match to win", all you have to do is reverse the order you are iterating the list.

var dataLines = sheetData.Elements<Row>().ToList();

for (int i = dataLines.Count - 1; i >= 0; i--)
{
    var x = dataLines[i];
    dataDictionary[x.RowIndex.Value] = x.OuterXml;
}
  • If x.RowIndex.Value is unique, it doesn't matter which direction you iterate.
  • If it is important that the key is sorted in ascending order, you can use a SortedDictionary<TKey, TValue>.

But as others have pointed out, it seems odd that you have so many hit counts. There is probably recursion going on in your application that you need to track down.