异步任务比同步任务快多少

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();//          四川党性培训 www.sdganxun.cn    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快了两三倍!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值