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

本文通过代码示例对比了异步任务与同步任务在执行效率上的差异,发现异步任务在特定场景下能显著提升性能,尤其是在使用`Task.WaitAll`时。测试结果显示,第一个异步示例的性能大约是其他同步示例的六到七倍。同时,文章也提到了并行处理对于性能的提升,但并行与异步的比较并不直接相关。总结了不同方法的时间消耗,为理解和优化代码执行速度提供了参考。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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、付费专栏及课程。

余额充值