同步版本示例: namespace SyncSample { class MyDownloadString { Stopwatch sw = new Stopwatch(); public void DoRun() { const int LargeNumber = 6000000; sw.Star ...
同步版本示例:
namespace SyncSample { class MyDownloadString { Stopwatch sw = new Stopwatch(); public void DoRun() { const int LargeNumber = 6000000; sw.Start(); //網路任務 int t1 = CountCharacters(1, "https://www.baidu.com"); int t2 = CountCharacters(2, "https://www.sogou.com/"); //本機任務 CountToALargeNumber(1, LargeNumber); CountToALargeNumber(2, LargeNumber); CountToALargeNumber(3, LargeNumber); CountToALargeNumber(4, LargeNumber); Console.WriteLine("Chars in Baidu:{0}", t1); Console.WriteLine("Chars in Sougou:{0}", t2); } private int CountCharacters(int id, string url) { WebClient wc1 = new WebClient(); Console.WriteLine("Starting call {0}:{1,4:N0} ms", id, sw.Elapsed.TotalMilliseconds); string result = wc1.DownloadString(new Uri(url)); Console.WriteLine("call {0} completed:{1,4:N0} ms", id, sw.Elapsed.TotalMilliseconds); return result.Length; } private void CountToALargeNumber(int id, int value) { for (long i = 0; i < value; i++) ; Console.WriteLine("End counting {0}:{1,4:N0} ms", id, sw.Elapsed.TotalMilliseconds); } } class Program { static void Main(string[] args) { MyDownloadString ds = new MyDownloadString(); ds.DoRun(); Console.ReadLine(); //同步時,會按照如下順序執行 t1-t2-count1-count2-count3-count4,最耗時的任務發生在等待網站數據(starting call-call completed)的過程 //但如果能夠在等待的過程中就執行count1-4就會極大節省時間 } } }
耗時為:
Starting call 1: 0 ms
call 1 completed: 222 ms
Starting call 2: 222 ms
call 2 completed: 435 ms
End counting 1: 448 ms
End counting 2: 460 ms
End counting 3: 473 ms
End counting 4: 486 ms
Chars in Baidu:14462
Chars in Sougou:19466
對應非同步示例:
class MyDownLoadString { Stopwatch sw = new Stopwatch(); public void DoRun() { const int LargeNumber = 600000; sw.Start(); Task<int> t1 = CountCharactersAsync(1, "https://www.baidu.com"); Task<int> t2 = CountCharactersAsync(2, "http://www.sougou.com"); CountToALargeNumber(1, LargeNumber); CountToALargeNumber(2, LargeNumber); CountToALargeNumber(3, LargeNumber); CountToALargeNumber(4, LargeNumber); Console.WriteLine("Chars in Baidu:{0}", t1.Result); Console.WriteLine("Chars in Sougou:{0}", t2.Result); } private async Task<int> CountCharactersAsync(int id, string site) { WebClient wc = new WebClient(); Console.WriteLine("Starting call {0}:{1,4:N0} ms", id, sw.Elapsed.TotalMilliseconds); string result = await wc.DownloadStringTaskAsync(new Uri(site)); Console.WriteLine("Call {0} completed:{1,4:N0} ms", id, sw.Elapsed.TotalMilliseconds); return result.Length; } private void CountToALargeNumber(int id,int value) { for (long i = 0; i < value; i++) ; Console.WriteLine("End counting {0}:{1,4:N0} ms", id, sw.Elapsed.TotalMilliseconds); } } class Program { static void Main(string[] args) { MyDownLoadString ds = new MyDownLoadString(); ds.DoRun(); Console.ReadLine(); //對於耗時的網路任務,修改成了非同步執行的方式,於是任務執行演變成了t1 start,t2 start-count1-4-t1 completed/t2 completed //在等待網路任務的同時,完成了本機任務 //而這些任務,都是在主線程完成,並沒有創建其他線程 } }
耗時為:
Starting call 1: 1 ms
Starting call 2: 58 ms
End counting 1: 62 ms
End counting 2: 63 ms
End counting 3: 64 ms
End counting 4: 65 ms
Call 2 completed: 199 ms
Call 1 completed: 243 ms
Chars in Baidu:14462
Chars in Sougou:16597
可見有效的壓縮了時間,這是因為4次CountToALargeNumber都是在等待網站響應的過程中完成的,而這些工作都是在主線程完成的,並沒有創建額外的線程。