zoukankan      html  css  js  c++  java
  • TPL

    namespace TPLTest
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
                //var list = testFillParallel();
                //int i = list.Count();
    
                Stopwatch watch = new Stopwatch();
                watch.Start();
                for (int i = 1; i < 5; i++)
                {
                    ProcessLongTime(i);
                }
                //watch.Stop();
                this.label1.Text = string.Format("顺序执行用时:" + watch.ElapsedMilliseconds);
    
                watch.Restart();
                //watch.Start();
                Parallel.For(1, 5, p =>
                {
                    ProcessLongTime(p);
                    //MessageBox.Show(p.ToString());
                });
                //watch.Stop();
                this.label2.Text = string.Format("并行执行用时:" + watch.ElapsedMilliseconds);
    
                //watch.Restart();
                ////watch.Start();
                //for (int i = 1; i <= 5; i++)
                //{
                //    Task myTask = new Task(obj => ProcessLongTime((int)obj), i);
                //    myTask.Start();
                //}
                ////watch.Stop();
                //this.label3.Text = string.Format("并行Task执行用时:" + watch.ElapsedMilliseconds);
    
                watch.Restart();
                int count = 5;
                int results;
                Semaphore semaphore = new Semaphore(0, count);
                System.Threading.Tasks.Parallel.For(0, count, i =>
                {
                    results = ProcessLongTimeI(i);
                    semaphore.Release();
                });
    
                //for (var i = 0; i <= count; i++)
                //{
                //    semaphore.WaitOne();
                //    //Console.WriteLine("Got " + i);
                //}
                watch.Stop();
                this.label3.Text = string.Format("并行Task执行用时:" + watch.ElapsedMilliseconds);
            }
    
    
            private  IEnumerable<Person> testFillParallel()
            {
                //var list = new List<Person>(9);
                var list = new BlockingCollection<Person>(9);  //必须使用线程安全的集合类型
    
                Enumerable.Range(1, 999).AsParallel().ForAll(n =>
                    {
                        var name = "Person" + n%9;
                        if (list.Count(p => p.Name == name) < 1) list.Add(new Person {Id = n, Name = name});
                    });
                this.label1.Text=string.Format("Person's count is {0}", list.Count);
                return list;
            }
    
            private void ProcessLongTime(int mi)
            {
                for (int i = 0; i < 10000000; i++)
                {
                    i++;
                    i--;
                }
            }
    
            private int ProcessLongTimeI(int mi)
            {
                for (int i = 0; i < 10000000; i++)
                {
                    i++;
                    i--;
                }
                return mi;
            }
    
            private void button2_Click(object sender, EventArgs e)
            {
                List<int> ls=new List<int>();
                for (int i = 0; i < 5; i++)
                {
                    ls.Add(i);
                }
    
                Stopwatch watch = new Stopwatch();
                watch.Start();
                foreach (int i in ls)
                {
                    ProcessLongTime(i);
                }
                this.label1.Text = string.Format("顺序执行用时:" + watch.ElapsedMilliseconds);
    
                watch.Restart();
                Parallel.ForEach(ls,p=>
                {
                    ProcessLongTime(p);
                    //MessageBox.Show(p.ToString());
                });
                this.label2.Text = string.Format("并行执行用时:" + watch.ElapsedMilliseconds);
    
                watch.Restart();
                int count = 5;
                int results;
                Semaphore semaphore = new Semaphore(0, ls.Count);
                System.Threading.Tasks.Parallel.ForEach(ls, p =>
                {
                    results = ProcessLongTimeI(p);
                    semaphore.Release();
                });
    
                watch.Stop();
                this.label3.Text = string.Format("并行Task执行用时:" + watch.ElapsedMilliseconds);
            }
        }
  • 相关阅读:
    oracle(Xe)数据库远程连接需修改配置参数
    oracl 权限循环查询
    控件网站
    java常用类(1)
    关于webdriver和谷歌浏览器的那些事
    2020年第27周,24.75h,完成计算智能/物联网/数据挖掘大作业
    2020年第26周,24.75h,计算智能的大小作业
    2020年第25周,25.5h,随机过程考试、report和计算智能作业
    2020年24周,11.75h,以完成作业和考试为主,看了一点点论文
    2020年第23周,11h,努力完成课程作业
  • 原文地址:https://www.cnblogs.com/zeroone/p/3285601.html
Copyright © 2011-2022 走看看