I am currently facing a performance problem with the following code.
private int globalType1 = 1;
private float globalType2 = 0.0342f;
private Dictionary<string, Tuple<int, float>> dt = new Dictionary<string, Tuple<int, float>>();
foreach (string file in Files) //assume 100 distinct files
{
//GetType1AndType2 is thread safe
Tuple<int, float> ift = GetType1AndType2(file, 0); //here 0 is the version of the file.
if (ift.Item1 == globalType1 && ift.Item2 == globalType2)
{
dt.Add(file + "_0", fftandFreq); //Key = <FileName_Version> ; Value = Tuple<Type1, Type2>
}
}
How can I accomplish this in parallel.
You can use
Parallel.Foreach
:Make sure to lock your dictionary or use a thread-safe dictionary type. Whether this helps your performance or not depends on the actions take inside. If it is I/O based it might not benefit that much, but you can test that.