用foreach (DictionaryEntry de in hashtable1)多核下想优化程序,如何
用Parallel.ForEach或Parallel.For实现,望给出代码实例,不胜感激!

解决方案 »

  1.   

    http://stackoverflow.com/questions/1657874/using-a-hashtable-inside-a-parallel-foreach
      

  2.   


    Dictionary<int, string> dic = new Dictionary<int, string>();            for (int i = 0; i < 1000; i++)
                {
                    dic.Add(i, "1");
                }
                Stopwatch sw = new Stopwatch();            sw.Start();
                foreach (var kv in dic)
                {
                    System.Threading.Thread.Sleep(1);
                }
                sw.Stop();
                Console.WriteLine("花费时间:{0}", sw.ElapsedMilliseconds);            sw.Restart();
                Parallel.ForEach(dic, _ => { System.Threading.Thread.Sleep(1); });
                sw.Stop();
                Console.WriteLine("花费时间:{0}", sw.ElapsedMilliseconds);