Visual Studio 中怎么利用Parallel类实现并行计算,很多新手对此不是很清楚,为了帮助大家解决这个难题,下面小编将为大家详细讲解,有这方面需求的人可以来学习下,希望你能有所收获。
Parallel类
Parallel类就是TPL中的一个用于支持并行计算的类。Parallel类提供了诸多的静态函数,只需要简单的函数调用,我们就可以对常用的for循环,foreach循环进行并行化。下面我们通过一些实际的例子,来看看如何利用这个类将我们的应用程序并行化,以吃上多核这“免费的午餐”。
创建示例项目
为了演示如何将一个现有的项目并行化,我们需要先创建一个示例项目。在这个项目中,我们将模拟对数据的串行操作,然后介绍如何利用Parallel类将对数据的串行操作并行化,以充分利用多核CPU的优势,从而提升应用程序的性能。在Visual Studio 2010中,我们新创建一个Visual C#的控制台应用程序。然后在这个项目中添加一个类Employee,其实现代码如下:
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ParallelDemo { // 职员类 public class Employee { public string FirstName { get; set; } public string LastName { get; set; } public string Address { get; set; } public DateTime HireDate { get; set; } public int EmployeeID { get; set; } // 模拟对数据的处理 public static decimal Process(Employee employee) { Console.WriteLine(”Processing {0}”, employee.EmployeeID); // 产生一个随机数 // 用以表示处理当前数据需要的时间 var rand = new Random(DateTime.Now.Millisecond); var delay = rand.Next(1, 5); var count = 0; var process = true; while (process) { System.Threading.Thread.Sleep(1000); count++; if (count >= delay) process = false; } return delay; } } // 职员列表类 // 这是我们需要处理的数据 public class EmployeeList : List { public EmployeeList() { // 将职员添加到列表中 Add(new Employee { EmployeeID = 1, FirstName = “张”, LastName = “三”, HireDate = DateTime.Parse(”1/1/2007″) }); Add(new Employee { EmployeeID = 2, FirstName = “李”, LastName = “四”, HireDate = DateTime.Parse(”3/15/2006″) }); Add(new Employee { EmployeeID = 3, FirstName = “王”, LastName = “麻子”, HireDate = DateTime.Parse(”6/17/2005″) }); Add(new Employee { EmployeeID = 4, FirstName = “赵”, LastName = “匡胤”, HireDate = DateTime.Parse(”3/19/2000″) }); Add(new Employee { EmployeeID = 5, FirstName = “钱”, LastName = “进”, HireDate = DateTime.Parse(”7/17/2003″) }); Add(new Employee { EmployeeID = 6, FirstName = “孙”, LastName = “俊鹏”, HireDate = DateTime.Parse(”9/13/2005″) }); Add(new Employee { EmployeeID = 7, FirstName = “李”, LastName = “明”, HireDate = DateTime.Parse(”12/3/2002″) }); Add(new Employee { EmployeeID = 8, FirstName = “周”, LastName = “大勇”, HireDate = DateTime.Parse(”7/1/2008″) }); Add(new Employee { EmployeeID = 9, FirstName = “吴”, LastName = “明子”, HireDate = DateTime.Parse(”1/7/2008″) }); Add(new Employee { EmployeeID = 10, FirstName = “郑”, LastName = “邦万”, HireDate = DateTime.Parse(”11/1/2001″) }); Add(new Employee { EmployeeID = 11, FirstName = “王”, LastName = “朝”, HireDate = DateTime.Parse(”4/21/2006″) }); Add(new Employee { EmployeeID = 12, FirstName = “冯”, LastName = “玛丽”, HireDate = DateTime.Parse(”7/19/2006″) }); Add(new Employee { EmployeeID = 13, FirstName = “陈”, LastName = “良乔”, HireDate = DateTime.Parse(”3/9/2001″) }); Add(new Employee { EmployeeID = 14, FirstName = “褚”, LastName = “春晖”, HireDate = DateTime.Parse(”7/15/2005″) }); Add(new Employee { EmployeeID = 15, FirstName = “卫”, LastName = “斯理”, HireDate = DateTime.Parse(”8/6/2003″) }); Add(new Employee { EmployeeID = 16, FirstName = “蒋”, LastName = “中正”, HireDate = DateTime.Parse(”5/18/2005″) }); Add(new Employee { EmployeeID = 17, FirstName = “沈”, LastName = “洋洋”, HireDate = DateTime.Parse(”8/5/2002″) }); Add(new Employee { EmployeeID = 18, FirstName = “韩”, LastName = “斌”, HireDate = DateTime.Parse(”10/1/2006″) }); Add(new Employee { EmployeeID = 19, FirstName = “杨”, LastName = “雪”, HireDate = DateTime.Parse(”12/7/2002″) }); Add(new Employee { EmployeeID = 20, FirstName = “朱”, LastName = “辉”, HireDate = DateTime.Parse(”3/30/2001″) }); DateTime.Parse(”12/7/2002″) }); Add(new Employee { EmployeeID = 20, FirstName = “朱”, LastName = “辉”, HireDate = DateTime.Parse(”3/30/2001″) }); } } } } } }
看完上述内容是否对您有帮助呢?如果还想对相关知识有进一步的了解或阅读更多相关文章,请关注编程网行业资讯频道,感谢您对编程网的支持。