ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

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

2022-04-04 01:33:51  阅读:145  来源: 互联网

标签:异步 同步 Console var 任务 resultResponse stopwatch new result


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

标签:异步,同步,Console,var,任务,resultResponse,stopwatch,new,result
来源: https://www.cnblogs.com/morec/p/16098209.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有