Click here to Skip to main content
12,998,423 members (88,677 online)
Rate this:
Please Sign up or sign in to vote.
See more:
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)
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]
Posted 5-Nov-12 7:23am
Updated 5-Nov-12 7:25am
wizardzz 5-Nov-12 15:43pm
Out of curiosity, how many cores does the cpu have?
Member 9574034 7-Nov-12 3:18am
One cpu Intel i7-950 (4 core, 8 thread)
Matt T Heffron 5-Nov-12 16:55pm
Unless I'm mistaken, this is creating a potentially large number of Tasks. There are the 10 Tasks being explicitly created, but the Parallel.ForEach and Parallel.For create Tasks for each "iteration" of the corresponding loops. If you don't have many cores in the computer, then this is ADDING the Tasking overhead. Also, whatever thread-protection method is being used in the ConcurrentDictionary class may be causing a very large number of context switches between these Tasks, slowing the overall performance.
Member 9574034 7-Nov-12 3:32am
Could you please give me some solution based on one CPU Intel I7-950? How can I modify my code to increase speed and performance? How many tasks should I use and How? I mean is it worthwhile to break 10 tasks to 5 two tasks?
Thank you.
wizardzz 7-Nov-12 9:54am
I'm suspecting context switches to be a big part of this.

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

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web02 | 2.8.170622.1 | Last Updated 5 Nov 2012
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100