VB.net 2010 视频教程 VB.net 2010 视频教程 python基础视频教程
SQL Server 2008 视频教程 c#入门经典教程 Visual Basic从门到精通视频教程
当前位置:
首页 > 编程开发 > c#教程 >
  • C#教程之并行编程和任务(一)(2)

本站最新发布   C#从入门到精通
试听地址  
https://www.xin3721.com/eschool/CSharpxin3721/

List<Test>(); for (int i = 0; i < 500; i++) { Test test = new Test(); test.Name = "Name" + i.ToString(); test.Index = "Index" + i.ToString(); test.Time = DateTime.Now; tests.Add(test); } Console.WriteLine("TaskFive_One 500条数据第一个方法 执行完成"); } public static void TaskFive_Two() { List<Test> tests = new List<Test>(); for (int i = 500; i < 1000; i++) { Test test = new Test(); test.Name = "Name" + i.ToString(); test.Index = "Index" + i.ToString(); test.Time = DateTime.Now; tests.Add(test); } Console.WriteLine("TaskFive_Two 500条数据第二个方法 执行完成"); } public static void TaskFive_Three() { List<Test> tests = new List<Test>(); for (int i = 1000; i < 1500; i++) { Test test = new Test(); test.Name = "Name" + i.ToString(); test.Index = "Index" + i.ToString(); test.Time = DateTime.Now; tests.Add(test); } Console.WriteLine("TaskFive_Three 500条数据第三个方法 执行完成"); } public static void TaskFive_Four() { List<Test> tests = new List<Test>(); for (int i = 1500; i < 2000; i++) { Test test = new Test(); test.Name = "Name" + i.ToString(); test.Index = "Index" + i.ToString(); test.Time = DateTime.Now; tests.Add(test); } Console.WriteLine("TaskFive_Four 500条数据第四个方法 执行完成"); } public static void TaskOnethousand_One() { List<Test> tests = new List<Test>(); for (int i = 0; i < 1000; i++) { Test test = new Test(); test.Name = "Name" + i.ToString(); test.Index = "Index" + i.ToString(); test.Time = DateTime.Now; tests.Add(test); } Console.WriteLine("TaskOnethousand_One 1000条数据第一个方法 执行完成"); } public static void TaskOnethousand_Two() { List<Test> tests = new List<Test>(); for (int i = 1000; i < 2000; i++) { Test test = new Test(); test.Name = "Name" + i.ToString(); test.Index = "Index" + i.ToString(); test.Time = DateTime.Now; tests.Add(test); } Console.WriteLine("TaskOnethousand_Two 1000条数据第二个方法 执行完成"); } public static void TaskOnethousand_Three() { List<Test> tests = new List<Test>(); for (int i = 2000; i < 3000; i++) { Test test = new Test(); test.Name = "Name" + i.ToString(); test.Index = "Index" + i.ToString(); test.Time = DateTime.Now; tests.Add(test); } Console.WriteLine("TaskOnethousand_Three 1000条数据第三个方法 执行完成"); } public static void TaskOnethousand_Four() { List<Test> tests = new List<Test>(); for (int i = 3000; i < 4000; i++) { Test test = new Test(); test.Name = "Name" + i.ToString(); test.Index = "Index" + i.ToString(); test.Time = DateTime.Now; tests.Add(test); } Console.WriteLine("TaskOnethousand_Four 1000条数据第四个方法 执行完成"); } #endregion

 

        static void Main(string[] args)
        {
            Stopwatch swTest = new Stopwatch();
            swTest.Start();
            Thread.Sleep(3000);
            TaskFive_One();
            TaskFive_Two();
            TaskFive_Three();
            TaskFive_Four();
            swTest.Stop();
            Console.WriteLine("500条数据  顺序编程所耗时间:" + swTest.ElapsedMilliseconds);


            //五百条数据并行完成 
            swTest.Restart();
            Thread.Sleep(3000);
            Parallel.Invoke(() => TaskFive_One(), () => TaskFive_Two(), () => TaskFive_Three(), () => TaskFive_Four());
            swTest.Stop();
            Console.WriteLine("500条数据  并行编程所耗时间:" + swTest.ElapsedMilliseconds);


            //一千条数据顺序完成 
            swTest.Restart();
            Thread.Sleep(3000);
            TaskOnethousand_One();
            TaskOnethousand_Two();
            TaskOnethousand_Three();
            TaskOnethousand_Four();
            swTest.Stop();
            Console.WriteLine("1000条数据  顺序编程所耗时间:" + swTest.ElapsedMilliseconds);


            //一千条数据并行完成 
            swTest.Restart();
            Thread.Sleep(3000);
            Parallel.Invoke(() => TaskOnethousand_One(), () => TaskOnethousand_Two(), () => TaskOnethousand_Three(), () => TaskOnethousand_Four());
            swTest.Stop();
            Console.WriteLine("1000条数据  并行编程所耗时间:" + swTest.ElapsedMilliseconds);
        }

 

 

  我们看下我们修改共享资源后,对于500条数据的运行结果,顺序编程比并行编程还是要快点,但是在1000条数据的时候并行编程就明显比顺序编程要快了。而且在测试中并行编程的运行顺序也是不固定的。我们在日常编程中我们需要衡量我们的应用是否需要并行编程,不然可能造成更多的性能损耗。

项目源码地址

https://github.com/hulizong/Parallel_Task

相关教程
        
关于我们--广告服务--免责声明--本站帮助-友情链接--版权声明--联系我们       黑ICP备07002182号