设为首页 加入收藏

TOP

Parallel.For 平行算法 使用
2017-10-10 12:31:52 】 浏览:6405
Tags:Parallel.For 平行 算法 使用

之前看到Parallel的用法,觉得很高深,很腻害,今天专门抽空研究了一下,发现还是很easy的。

.NET Framework 4.0 新加的功能,所以4.0之前的无法使用哦。

下面介绍一下,Parallel称为 平行算法,用白话说,就是为了充分利用电脑多核处理器的优势,使得每隔核心都可以努力干活,不让他们闲着,来提高运行效率。

不过使用需要注意几点:

1:Parallel 并行处理时 如果涉及到共享资源的话,使用要很小心,因为并行同时访问共享资源,就会出现不确定的状态,非要使用,可以加锁来解决;

2:Parallel中不管是For还是Foreach,处理都是乱序的,并不是按照顺序来处理,所以要当心;

3:如果列表或者循环次数较少,不建议使用Parallel,因为创建线程资源之类的处理会浪费很多事件和资源;

4:同样,如果内部处理逻辑非常简单,也不建议使用Parallel,因为创建资源的耗费并不值得,比较复杂的处理逻辑,可以考虑Parallel

 

talk is cheap,show you code

        public static void parallelFunc()
        {
            DateTime startTime;
            TimeSpan resultTime;
            List<int> testList = new List<int>();
            for (int i = 0; i < 10; i++)
            {
                testList.Add(i);
            }
            startTime = System.DateTime.Now;
            loop1(testList);
            resultTime = System.DateTime.Now - startTime;
            Console.WriteLine("一般for循环耗时:" + resultTime.TotalMilliseconds);
            startTime = System.DateTime.Now;
            loop2(testList);
            resultTime = System.DateTime.Now - startTime;
            Console.WriteLine("一般foreach循环耗时:" + resultTime.TotalMilliseconds);
            startTime = System.DateTime.Now;
            loop3(testList);
            resultTime = System.DateTime.Now - startTime;
            Console.WriteLine("并行for循环耗时:" + resultTime.TotalMilliseconds);
            startTime = System.DateTime.Now;
            loop4(testList);
            resultTime = System.DateTime.Now - startTime;
            Console.WriteLine("并行foreach循环耗时:" + resultTime.TotalMilliseconds);
            Console.ReadLine();
        }

        #region  Parallel 循环
        //普通的for循环
        static void loop1(List<int> source)
        {
            int count = source.Count();
            for (int i = 0; i < count; i++)
            {
                System.Threading.Thread.Sleep(100);
            }
        }

        //普通的foreach循环
        static void loop2(List<int> source)
        {
            foreach (int item in source)
            {
                System.Threading.Thread.Sleep(100);
            }
        }

        //并行的for循环
        static void loop3(List<int> source)
        {
            int count = source.Count();
            Parallel.For(0, count, index =>
            {
                Console.WriteLine($"Parallel.For:{index}");
                System.Threading.Thread.Sleep(100);
            });
        }

        //并行的foreach循环
        static void loop4(List<int> source)
        {
            Parallel.ForEach(source, item =>
            {
                Console.WriteLine($"Parallel.ForEach:{item}");
                System.Threading.Thread.Sleep(100);
            });
        }

        #endregion
    }
View Code

 

运行结果:

 

可以看到,执行效率依次为:  Parallel.ForEach >  Parallel.For > 普通ForEach > 普通For,且Parallel是无序运行的

具体效率应该取决于电脑的CPU核心数,我的是4核,所以大概是4倍左右

 

】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇Revit二次开发基础-在附加模块中.. 下一篇SVN服务器搭建

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目