Apress Introducing dot NET 4 0 with Visual Studio 2010_2
Số trang: 45
Loại file: pdf
Dung lượng: 1.81 MB
Lượt xem: 11
Lượt tải: 0
Xem trước 5 trang đầu tiên của tài liệu này:
Thông tin tài liệu:
Nếu bạn muốn làm việc với một ví dụ thực tế hơn, hãy xem các ví dụ từ nhóm song song, bạn sẽ tìm thấy ray tracing tuyệt vời và các ví dụ liên quan đến toán học khác. Lưu ý rằng gọi Thread.Sleep () sẽ liên quan đến một chuyển ngữ cảnh (một hoạt động đắt tiền cho CPU), vì vậy nó có thể làm chậm các ứng dụng mẫu xuống nhiều hơn so với thực hiện công việc có thể có.
Nội dung trích xuất từ tài liệu:
Apress Introducing dot NET 4 0 with Visual Studio 2010_2 CHAPTER 5 PARALLELIZATION AND THREADING ENHANCEMENTS factorials or walk trees of data, but I think this distracts (at least initially) from understanding the basics. If you want to work with a more realistic example, take a look at the examples from the parallel team; you will find excellent ray tracing and other math related examples. Note that calling the Thread.Sleep() method will involve a context switch (an expensive operation for the CPU), so it might slow the sample application down more than performing work might have. Create a new console application called Chapter5.HelloParalleland add the following using 1. directives: using System.Diagnostics; using System.Threading.Tasks; Amend Program.cs to the following code: 2. class Program { public static List Stocks = new List(); static void Main(string[] args) { double serialSeconds = 0; double parallelSeconds = 0; Stopwatch sw = new Stopwatch(); PopulateStockList(); sw = Stopwatch.StartNew(); RunInSerial(); serialSeconds = sw.Elapsed.TotalSeconds; sw = Stopwatch.StartNew(); RunInParallel(); parallelSeconds = sw.Elapsed.TotalSeconds; Console.WriteLine( Finished serial at {0} and took {1}, DateTime.Now, serialSeconds); Console.WriteLine( Finished parallel at {0} and took {1}, DateTime.Now, parallelSeconds); Console.ReadLine(); } private static void PopulateStockList() { Stocks.Add(new StockQuote { ID = 1, Company = Microsoft, Price = 5.34m }); Stocks.Add(new StockQuote { ID = 2, Company = IBM, Price = 1.9m }); Stocks.Add(new StockQuote { ID = 3, Company = Yahoo, Price = 2.34m }); Stocks.Add(new StockQuote { ID = 4, Company = Google, Price = 1.54m }); Stocks.Add(new StockQuote { ID = 5, Company = Altavista, Price = 4.74m }); Stocks.Add(new StockQuote { ID = 6, Company = Ask, Price = 3.21m });102 CHAPTER 5 PARALLELIZATION AND THREADING ENHANCEMENTS Stocks.Add(new StockQuote { ID = 7, Company = Amazon, Price = 20.8m }); Stocks.Add(new StockQuote { ID = 8, Company = HSBC, Price = 54.6m }); Stocks.Add(new StockQuote { ID = 9, Company = Barclays, Price = 23.2m }); Stocks.Add(new StockQuote { ID = 10, Company = Gilette, Price = 1.84m }); } private static void RunInSerial() { for (int i = 0; i < Stocks.Count; i++) { Console.WriteLine(Serial processing stock: {0},Stocks[i].Company); StockService.CallService(Stocks[i]); Console.WriteLine(); } } private static void RunInParallel() { Parallel.For(0, Stocks.Count, i => { Console.WriteLine(Parallel processing stock: {0}, Stocks[i].Company); StockService.CallService(Stocks[i]); Console.WriteLine(); }); } } Create a new class called StockQuote and add the following code: 3. Listing 5-1. Parallel For Loop public class StockQuote { public int ID {get; set;} public string Company { get; set; } public decimal Price{get; set;} } Create a new class called StockService and enter the following code: 4. public class StockService { public static decimal CallService(StockQuote Quote) { Console.WriteLine(Executing long task for {0}, Quote.Company); var rand = new Random(DateTime.Now.Millisecond); System.Threading.Thread.Sleep(1000); return Convert.ToDecimal(rand.NextDouble()); } } Press F5 to run the code. When I run the code on my machine I receive the output shown inFigure ...
Nội dung trích xuất từ tài liệu:
Apress Introducing dot NET 4 0 with Visual Studio 2010_2 CHAPTER 5 PARALLELIZATION AND THREADING ENHANCEMENTS factorials or walk trees of data, but I think this distracts (at least initially) from understanding the basics. If you want to work with a more realistic example, take a look at the examples from the parallel team; you will find excellent ray tracing and other math related examples. Note that calling the Thread.Sleep() method will involve a context switch (an expensive operation for the CPU), so it might slow the sample application down more than performing work might have. Create a new console application called Chapter5.HelloParalleland add the following using 1. directives: using System.Diagnostics; using System.Threading.Tasks; Amend Program.cs to the following code: 2. class Program { public static List Stocks = new List(); static void Main(string[] args) { double serialSeconds = 0; double parallelSeconds = 0; Stopwatch sw = new Stopwatch(); PopulateStockList(); sw = Stopwatch.StartNew(); RunInSerial(); serialSeconds = sw.Elapsed.TotalSeconds; sw = Stopwatch.StartNew(); RunInParallel(); parallelSeconds = sw.Elapsed.TotalSeconds; Console.WriteLine( Finished serial at {0} and took {1}, DateTime.Now, serialSeconds); Console.WriteLine( Finished parallel at {0} and took {1}, DateTime.Now, parallelSeconds); Console.ReadLine(); } private static void PopulateStockList() { Stocks.Add(new StockQuote { ID = 1, Company = Microsoft, Price = 5.34m }); Stocks.Add(new StockQuote { ID = 2, Company = IBM, Price = 1.9m }); Stocks.Add(new StockQuote { ID = 3, Company = Yahoo, Price = 2.34m }); Stocks.Add(new StockQuote { ID = 4, Company = Google, Price = 1.54m }); Stocks.Add(new StockQuote { ID = 5, Company = Altavista, Price = 4.74m }); Stocks.Add(new StockQuote { ID = 6, Company = Ask, Price = 3.21m });102 CHAPTER 5 PARALLELIZATION AND THREADING ENHANCEMENTS Stocks.Add(new StockQuote { ID = 7, Company = Amazon, Price = 20.8m }); Stocks.Add(new StockQuote { ID = 8, Company = HSBC, Price = 54.6m }); Stocks.Add(new StockQuote { ID = 9, Company = Barclays, Price = 23.2m }); Stocks.Add(new StockQuote { ID = 10, Company = Gilette, Price = 1.84m }); } private static void RunInSerial() { for (int i = 0; i < Stocks.Count; i++) { Console.WriteLine(Serial processing stock: {0},Stocks[i].Company); StockService.CallService(Stocks[i]); Console.WriteLine(); } } private static void RunInParallel() { Parallel.For(0, Stocks.Count, i => { Console.WriteLine(Parallel processing stock: {0}, Stocks[i].Company); StockService.CallService(Stocks[i]); Console.WriteLine(); }); } } Create a new class called StockQuote and add the following code: 3. Listing 5-1. Parallel For Loop public class StockQuote { public int ID {get; set;} public string Company { get; set; } public decimal Price{get; set;} } Create a new class called StockService and enter the following code: 4. public class StockService { public static decimal CallService(StockQuote Quote) { Console.WriteLine(Executing long task for {0}, Quote.Company); var rand = new Random(DateTime.Now.Millisecond); System.Threading.Thread.Sleep(1000); return Convert.ToDecimal(rand.NextDouble()); } } Press F5 to run the code. When I run the code on my machine I receive the output shown inFigure ...
Tìm kiếm theo từ khóa liên quan:
thủ thuật máy tính tài liệu công nghệ thông tin lập trình máy tính mẹo máy tính cài đặt máy tínhGợi ý tài liệu liên quan:
-
Top 10 mẹo 'đơn giản nhưng hữu ích' trong nhiếp ảnh
11 trang 317 0 0 -
Làm việc với Read Only Domain Controllers
20 trang 305 0 0 -
Thêm chức năng hữu dụng cho menu chuột phải trên Windows
4 trang 289 0 0 -
70 trang 251 1 0
-
Bài giảng Tin học lớp 11 bài 1: Giới thiệu ngôn ngữ lập trình C#
15 trang 238 0 0 -
Tổng hợp lỗi Win 8 và cách sửa
3 trang 233 0 0 -
Sửa lỗi các chức năng quan trọng của Win với ReEnable 2.0 Portable Edition
5 trang 214 0 0 -
Giáo trình Bảo trì hệ thống và cài đặt phần mềm
68 trang 207 0 0 -
Tổng hợp 30 lỗi thương gặp cho những bạn mới sử dụng máy tính
9 trang 204 0 0 -
UltraISO chương trình ghi đĩa, tạo ổ đĩa ảo nhỏ gọn
10 trang 204 0 0