• 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);
            }
        }
  • 相关阅读:
    理解MySQL——索引与优化
    一个简单的效果可以拖动的div
    jsp中验证码的实现
    java经典算法四十题
    java.util.Date和java.sql.Date的区别及应用
    懒人笔记memcache配置(php)
    很奇怪的SQL问题,top和inner join一起使用时出现排序问题
    aspnet_regiis一些技巧
    webservice服务输出xml格式自定义格式内容
    FCK配置中文版(转自网络,未知来源)
  • 原文地址:https://www.cnblogs.com/zeroone/p/3285601.html
Copyright © 2020-2023  润新知