flo
flo

Reputation: 671

Performance improvement for Lambda using Array.Contains?

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?

Upvotes: 1

Views: 319

Answers (2)

Slai
Slai

Reputation: 22876

I can't think of anything simple, but as a final resort you should be able to save another about 20% by avoiding the LINQ and lambda overhead:

private static IEnumerable<Item> GetSimilarItems(int days, string type, 
                                                 float counterOne, float counterTwo)
{
    var similarSubItems = new HashSet<string>();
    var c9 = counterOne * 0.9;
    var c1 = counterOne * 1.1;

    if (days > 180)
    {
        foreach (var p in SubItemCache.Values)
            if (p.CounterOne >= c9 && p.CounterOne <= c1)
                similarSubItems.Add(p.ID);
    }
    else
    {
        foreach (var p in SubItemCache.Values)
            if (p.CounterTwo >= c9 && p.CounterTwo <= c1)
                similarSubItems.Add(p.ID);
    }

    var days0 = days - 5;
    var days1 = days + 5;

    foreach (var p in ItemCache.Values) 
        if (p.days >= days0 && p.days <= days1 
                            && p.Type == type && similarSubItems.Contains(p.Key))
            yield return p;
}

Upvotes: 0

Scott Chamberlain
Scott Chamberlain

Reputation: 127563

Depending on it's size, swap out the string[] with a HashSet<string> and use the .Contains method on that. It will have significantly faster lookup times.

Last time I tested, i found that around 15 items in the collection is when you had faster lookup times with a Hash Set if you compared a straight lookup vs the overhead of building the Hash Set + the lookup.

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)
{
    HashSet<string> similarSubItems;

    if (days > 180)
    {
        similarSubItems = new HashSet<string>(SubItemCache.Values
            .Where(p => p.CounterOne >= counterOne * 0.9 
                && p.CounterOne <= counterOne * 1.1)
            .Select(o => o.ID));
    }
    else
    {
        similarSubItems = new HashSet<string>(SubItemCache.Values
            .Where(p => p.CounterTwo >= counterTwo * 0.9 
                && p.CounterTwo <= counterTwo * 1.1)
            .Select(o => o.ID));
    }

    var selection = ItemCache.Values.Where(p => p.days >= days - 5 && p.days <= days + 5
                                          && p.Type == type
                                          && similarSubItems.Contains(p.Key));

    return selection;
}

Upvotes: 2

Related Questions