using System.Diagnostics; namespace TestStatue{ internal class Program { static async Task Main( string [] args) { DateTime now = DateTime.Now; var t1 = new ListTaskHttpResponseMessage (); for ( int i = 0 ; i 100 ; i++ ) { var resultRespons
using System.Diagnostics; namespace TestStatue { internal class Program { static async Task Main(string[] args) { DateTime now = DateTime.Now; var t1 = new List<Task<HttpResponseMessage>>(); for (int i = 0; i < 100; i++) { var resultResponse = new HttpClient().SendAsync(new HttpRequestMessage { RequestUri = new Uri("https://www.baidu.com") }); t1.Add(resultResponse); // var result = await resultResponse.Content.ReadAsStreamAsync(); } var t2 = new List<Task<Stream>>(); t1.ForEach(async x => { var res = await x; t2.Add(res.Content.ReadAsStreamAsync()); }); Task.WaitAll(t2.ToArray()); Console.WriteLine($"运行时间:{DateTime.Now.Subtract(now).TotalMilliseconds}"); } } } //using System.Diagnostics; //namespace TestStatue //{ // internal class Program // { // static async Task Main(string[] args) // { // Stopwatch stopwatch = new Stopwatch(); // stopwatch.Start(); // var t2 = new List<Task<Stream>>(); // for (int i = 0; i < 100; i++) // { // var resultResponse = await new HttpClient().SendAsync(new HttpRequestMessage { RequestUri = new Uri("https://www.baidu.com") }); // var result = resultResponse.Content.ReadAsStreamAsync(); // t2.Add(result); // } // Task.WaitAll(t2.ToArray()); // //Console.WriteLine(result); // stopwatch.Stop(); // Console.WriteLine($"运行时间:{stopwatch.ElapsedMilliseconds}"); // } // } //} //using System.Diagnostics; //namespace TestStatue //{ // internal class Program // { // static async Task Main(string[] args) // { // Stopwatch stopwatch = new Stopwatch(); // stopwatch.Start(); // for (int i = 0; i < 100; i++) // { // var resultResponse = await new HttpClient().SendAsync(new HttpRequestMessage { RequestUri = new Uri("https://www.baidu.com") }); // var result = await resultResponse.Content.ReadAsStreamAsync(); // } // //Console.WriteLine(result); // stopwatch.Stop(); // Console.WriteLine($"运行时间:{stopwatch.ElapsedMilliseconds}"); // } // } //} //using System.Diagnostics; //namespace TestStatue //{ // internal class Program // { // static void Main(string[] args) // { // Stopwatch stopwatch = new Stopwatch(); // stopwatch.Start(); // Parallel.For(0, 10, (i) => // { // var resultResponse = new HttpClient().Send(new HttpRequestMessage { RequestUri = new Uri("https://www.baidu.com") }); // var result = resultResponse.Content.ReadAsStream(); // }); // //Console.WriteLine(result); // stopwatch.Stop(); // Console.WriteLine($"运行时间:{stopwatch.ElapsedMilliseconds}"); // } // } //} //using System.Diagnostics; //namespace TestStatue //{ // internal class Program // { // static void Main(string[] args) // { // Stopwatch stopwatch = new Stopwatch(); // stopwatch.Start(); // for (int i = 0; i < 100; i++) // { // var resultResponse = new HttpClient().Send(new HttpRequestMessage { RequestUri = new Uri("https://www.baidu.com") }); // var result = resultResponse.Content.ReadAsStream(); // } // //Console.WriteLine(result); // stopwatch.Stop(); // Console.WriteLine($"运行时间:{stopwatch.ElapsedMilliseconds}"); // } // } //}
异步任务一直用,突然想测试下异步任务比同步任务快多少。就写了个下载的方法。因为获取string只有异步,所以过去stream,比较的时候无逻辑代码差异。
例子代码从下往上看,发现异步个同步执行时间是差不多的。
后面就加了并行,当然并行的话拿来比较没意义了。
后面就加入task.waitall,发现改善效果很明显。内部机智没深究!
这里单个例子用了两个等待,所以用起来又有点别扭。出现了第一个demo和第二个demo。
这里的执行时间有又差别!知识储备有限,稍微总结一下:
现有循环业务下第一个demo比最的两个快了六七倍,比第二个demo快了两三倍!