I have this function and I want to improve the performance. The bottleneck is at the end when selection is created and has probably to do with the Contains() function. I don't know a more efficient way to do that selection:
public static Dictionary<string, SubItem> SubItemCache = new Dictionary<string, SubItem>();
public static Dictionary<string, Item> ItemCache = new Dictionary<string, Item>();
private static IEnumerable<Item> GetSimilarItems(int days, string type,
float counterOne, float counterTwo)
{
string[] similarSubItems;
if (days > 180)
{
similarSubItems = SubItemCache.Values
.Where(p => p.CounterOne >= counterOne * 0.9
&& p.CounterOne <= counterOne * 1.1)
.Select(o => o.ID).ToArray();
}
else
{
similarSubItems = SubItemCache.Values
.Where(p => p.CounterTwo >= counterTwo * 0.9
&& p.CounterTwo <= counterTwo * 1.1)
.Select(o => o.ID).ToArray();
}
var selection = ItemCache.Values.Where(p => p.days >= days - 5 && p.days <= days + 5
&& p.Type == type
&& similarSubItems.Contains(p.Key));
return selection;
}
Is there a way to improve the function performance wise?