【发布时间】:2015-07-15 07:55:02
【问题描述】:
抱歉标题不好。我目前正在学习 TPL 并阅读 this 博客文章,其中指出
异步调用同步方法的能力对可伸缩性没有任何帮助,因为您通常仍会消耗与同步调用它相同数量的资源(实际上,您使用的资源要多一点,因为安排某些事情会产生开销)。
所以我想让我们试一试,我创建了使用WebClient 的DownloadStringTaskAsync 和DownloadString(同步)方法的演示应用程序。
我的演示应用程序有两种方法
-
下载HtmlNotAsyncInAsyncWay
这提供了围绕同步方法
DownloadString的异步方法包装器,它不应该很好地扩展。 -
下载HTMLCSAsync
这会调用异步方法 DownloadStringTaskAsync。
我从这两种方法创建了 100 个任务并比较了消耗的时间,发现选项 1 消耗的时间比第二个要少。为什么?
这是我的代码。
using System;
using System.Diagnostics;
using System.Net;
using System.Threading.Tasks;
public class Program
{
public static void Main()
{
const int repeattime = 100;
var s = new Sample();
var sw = new Stopwatch();
var tasks = new Task<string>[repeattime];
sw.Start();
for (var i = 0; i < repeattime; i++)
{
tasks[i] = s.DownloadHtmlNotAsyncInAsyncWay();
}
Task.WhenAll(tasks);
Console.WriteLine("==========Time elapsed(non natural async): " + sw.Elapsed + "==========");
sw.Reset();
sw.Start();
for (var i = 0; i < repeattime; i++)
{
tasks[i] = s.DownloadHTMLCSAsync();
}
Task.WhenAll(tasks);
Console.WriteLine("==========Time elapsed(natural async) : " + sw.Elapsed + "==========");
sw.Reset();
}
}
public class Sample
{
private const string Url = "https://www.google.co.in";
public async Task<string> DownloadHtmlNotAsyncInAsyncWay()
{
return await Task.Run(() => DownloadHTML());
}
public async Task<string> DownloadHTMLCSAsync()
{
using (var w = new WebClient())
{
var content = await w.DownloadStringTaskAsync(new Uri(Url));
return GetWebTitle(content);
}
}
private string DownloadHTML()
{
using (var w = new WebClient())
{
var content = w.DownloadString(new Uri(Url));
return GetWebTitle(content);
}
}
private static string GetWebTitle(string content)
{
int titleStart = content.IndexOf("<title>", StringComparison.InvariantCultureIgnoreCase);
if (titleStart < 0)
{
return null;
}
int titleBodyStart = titleStart + "<title>".Length;
int titleBodyEnd = content.IndexOf("</title>", titleBodyStart, StringComparison.InvariantCultureIgnoreCase);
return content.Substring(titleBodyStart, titleBodyEnd - titleBodyStart);
}
}
Here 是 dotnetfiddle 链接。
为什么第一个选项比第二次完成的时间短?
【问题讨论】:
标签: c# async-await task-parallel-library