Danh mục

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    
10.10.2023

Phí tải xuống: 12,000 VND Tải xuống file đầy đủ (45 trang) 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 ...

Tài liệu được xem nhiều: