-
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
试听地址 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
栏目列表
最新更新
C#中获取指定目录下所有目录的名称、全
Python中编写类的各种技巧和方法
解决pip使用异常No module named 'pip'
python(生成器)
服务器重启后启动Docker命令
单表序列化 和 单表反序列化
零基础小白入门IT开发指南
用Python复制文件的9个方法
Scrapy中的Request和日志分析
Python实现FTP文件定时自动下载
.Net Standard(.Net Core)实现获取配置信息
Linux PXE + Kickstart 自动装机
Shell 编程 基础
Shell 编程 条件语句
CentOS8-网卡配置及详解
Linux中LVM逻辑卷管理
1.数码相框-相框框架分析(1)
Ubuntu armhf 版本国内源
Linux中raid磁盘阵列
搭建简易网站
如何远程连接SQL Server数据库的图文教程
复制SqlServer数据库的方法
搜索sql语句
sql中返回参数的值
sql中生成查询的模糊匹配字符串
数据定义功能
数据操作功能
将Session值储存于SQL Server中
使用SQL Server数据库嵌套子查询的方法
SQL概述