Click here to Skip to main content
15,881,881 members
Please Sign up or sign in to vote.
4.67/5 (3 votes)
I'm looking into what the quickest way would be to add two dictionaries together.
The two dictionaries can contain the same keys, but some keys can also be unique.

Right now I have the following:
C#
private Dictionary<int, double> AddMaterialsDic(Dictionary<int, double> dic1, Dictionary<int, double> dic2)
{
    // Add Dic2 to Dic1
    foreach (KeyValuePair<int, double> kvp in dic2)
        if (dic1.ContainsKey(kvp.Key))
            dic1[kvp.Key] += kvp.Value;
        else
            dic1.Add(kvp.Key, kvp.Value);
    return dic1;
}



But this method takes about 30% of my execution time (it happens a few billion times /minute). So it's my main target at the moment to reduce my execution time if it's even possible.

Multithreading the method isn't possible in this case (need result before I can continue + it's fing .NET 2.0 we are talking about ^^). I also looked into OpenCL but it seems things like c[i] = a[i] + b[i] are faster if you do them in .NET because you can't gain the overhead back on such simple functions.

No lambda please, has to be .NET 2.0.
Posted
Comments
Yusuf 11-Mar-11 9:41am    
Interesting problem
#realJSOP 11-Mar-11 10:36am    
I changed my answer - see if the new solution is possible in .Net 2.0

If your desired result is a final list of unique items, you could try adding the items from each Dictionary to a HashSet, and go from there. I don't know if this is going to be any faster, but it's worth a shot.

You might be able to do this in .Net 2.0 - extension methods (typed off the top of my brain, so you may have to tweak it):

C#
public static class ExtensionMethods
{
    public static bool AddUnique(this Dictionary<T, T> dict, object key, object value)
    {
        bool added = true;
        if (this.Contains(key))
        {
            added = false;
            this[key].Value = value;
        }
        else
        {
            this.Add(key, value);
        }
        return added;
    }
 
    public static bool AddUnique(this Dictionary<T, T> dict, KeyValuePair<T, T> kvp)
    {
        return dict.AddUnique(kvp.Key, kvp.Value);
    }


EDIT #2 =============

You can do extension methods in.Net 2.0, but you have to define your own attribute:

C#
namespace System.Runtime.CompilerServices
{
    [AttributeUsage(AttributeTargets.Method, AllowMultiple = false, Inherited = false)]
    public class ExtensionAttribute : Attribute
    {
    }
}


And then add the attribute to each of your extension methods.
 
Share this answer
 
v5
Comments
Yusuf 11-Mar-11 9:35am    
If I am not mistaken HashSet was introduced in 3.5, that makes it outside his target Framework (2.0)
Toli Cuturicu 11-Mar-11 12:37pm    
Extension methods do not exist in .net 2.0 either.
#realJSOP 11-Mar-11 13:28pm    
See my 2nd update:
Toli Cuturicu 11-Mar-11 14:19pm    
Changed my 3 to a 5.
It is very rare for someone to actually use criticism instead of flaming out. Congrats!
#realJSOP 12-Mar-11 10:21am    
I try not to flame in the programming forums or Q/A unless someone is being a complete ass-hat, and if I think they're being one, I just delete their post which is the best response (I think). Besides, this goes back to my tip/trick that advises people to think outside their own box and to see if there's a solution on the net. I happened to remember seeing something about extension methods in .Net 2.0. I don't know if this is going to be any faster that what he's already doing, but it will at least be easier to maintain/comment.
Given your requiremnt of staying within 2.0, I am not sure how else you will tackle it, becuase all the possible solution will involve similar code using foreach.

If this is taking 30% of your execution time, I'd seriously look into upgrading to 4.0 provided
1. There is less code change
2. You execution can be improved drastically.

As you mentioned it, once to move to 4.0, there is lambda and the Dictionary class has more functionality, such as CopyTo
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 11-Mar-11 21:24pm    
Yusuf, I'm pretty much sure your note about v.2.0 is irrelevant; you probably forgot this version, but I do support it in all my Articles and most Answer. Foreach and anonymous methods and generics: this all is supported by 2.0. Lambda is not.
--SA
Yusuf 12-Mar-11 11:14am    
I am not sure what you saying. All I was saying is that, Since he is traversing his collection and that takes about 30% of his execution time. I feel that lambda might gain him something, but can't prove it with out writing the actual code and comparing the results.

All, I am saying is that, why don't you (the OP) seriously think of upgrading as the benefits might outweigh the hassle.

As for genrics and anonymous methods, I don't see how they can help his solve the problem, even though they are available to him.
Sergey Alexandrovich Kryukov 12-Mar-11 17:52pm    
I only say that v.2.0 should be enough. ?nonymous and generics are already in v.2.0 (useful or not, don't want to consider) and having lambda (which in not in v.2.0) is not important.
--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900