I have a lot of sortedlists<> in my program. I run my program for large number of repeats. In each repeat a lot of data add to the sorted lists and data from previous repeat also add to current list through loops.At first, number of data is small then the program is fast, but gradually amount of data increase and performance decrease. I want to increase the speed of my program therefore I decided to use Parallel task (each list as a task to add data to lists concurrently) and Parallel loop (to copy data from previous to current list) as well as concurrent dictionary (instead of sorted list). Not only did performance not increase but also it decreased radically. Add object to concurrent dictionary is very slow. Is it worthwhile to use parallel programming? If yes, Could you please give me a clue? I attached a part of my program.
class Segment<t,>
{
public ChrSegment()
{
}
public ConcurrentDictionary<t,> Zero { get; set; }
public ConcurrentDictionary<t,> One { get; set; }
public ConcurrentDictionary<t,> Two { get; set; }
public ConcurrentDictionary<t,> Three { get; set; }
public ConcurrentDictionary<t,> Four { get; set; }
public ConcurrentDictionary<t,> Five { get; set; }
public ConcurrentDictionary<t,> Six { get; set; }
public ConcurrentDictionary<t,> Seven { get; set; }
public ConcurrentDictionary<t,> Eight { get; set; }
public ConcurrentDictionary<t,> Nine { get; set; }
}
static void Main(string[] args)
{
var tasks = new Task[10]
{
Task.Factory.StartNew(()=>Segment.Zero = Loop(NewtmpZero),
Task.Factory.StartNew(()=>Segment.One = Loop(tmpOne)),
Task.Factory.StartNew(()=>Segment.Two = Loop(tmpTwo)),
Task.Factory.StartNew(()=>Segment.Three = Loop(tmpThree)),
Task.Factory.StartNew(()=>Segment.Four = Loop(tmpFour)),
Task.Factory.StartNew(()=>Segment.Five = Loop(tmpFive)),
Task.Factory.StartNew(()=>Segment.Six = Loop(tmpSix)),
Task.Factory.StartNew(()=>Segment.Seven = Loop(tmpSeven)),
Task.Factory.StartNew(()=>Segment.Eight = Loop(tmpEight)),
Task.Factory.StartNew(()=>Segment.Nine = GetPreGroup(tmpNine)
};
Task.WaitAll(tasks);
}
protected static ConcurrentDictionary<int,> Loop(SortedList<int,> tmp)
{
var Return= new ConcurrentDictionary<int,>();
Parallel.ForEach(tmp, item =>
{
Return.TryAdd(item.Key, item.Value);
});
return Return;
}
protected static ConcurrentDictionary<int,> GetPreGroup(SortedList<int,> Newtmp)
{
var Return = new ConcurrentDictionary<int,>();
Parallel.For(0, Newtmp.Count, l =>
{
Return.TryAdd(tmpMutation.Keys[l], tmp[tmp.Keys[l]]);
});
return Return;
}
[Edit]Code block added[/Edit]