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

Posted 星仔007

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了异步任务比同步任务快多少相关的知识,希望对你有一定的参考价值。

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快了两三倍!

以上是关于异步任务比同步任务快多少的主要内容,如果未能解决你的问题,请参考以下文章

同步OR异步?WebFlux开发真的比Servlet开发要快?顺便再科普下CompletableFuture

python-并发并行同步异步同步锁

python 同步异步,并发并行,同步锁

协程,事件,队列,同步,异步,回调函数

同步任务和异步任务

javascript 异步请求封装成同步请求