gpt4 book ai didi

c# - "synchronized"异步方法的开销是多少?

转载 作者:行者123 更新时间:2023-11-30 12:30:15 25 4
gpt4 key购买 nike

这是方法返回任务的基准,但在后台同步运行。

class MainClass
{
public static async Task<int> UsingAsyncModifier()
{
return 10;
}

public static Task<int> UsingTaskCompletionSource()
{
TaskCompletionSource<int> tcs = new TaskCompletionSource<int>();
tcs.SetResult(10);
return tcs.Task;
}

public static Task<int> UsingTaskFromResult()
{
return Task.FromResult(10);
}

public static void Main(string[] args)
{
DateTime t = DateTime.Now;
const int repeat = 10000; // Results volatile while repeat grows.
Console.WriteLine("Repeat {0} times.", repeat);

int j = 0;
for (int i = 0; i < repeat; i++)
{
j += UsingAsyncModifier().Result;
}
Console.WriteLine("UsingAsyncModifier: {0}", DateTime.Now - t);
t = DateTime.Now;

for (int i = 0; i < repeat; i++)
{
j += UsingTaskCompletionSource().Result;
}
Console.WriteLine("UsingTaskCompletionSource: {0}", DateTime.Now - t);
t = DateTime.Now;

for (int i = 0; i < repeat; i++)
{
j += UsingTaskFromResult().Result;
}
Console.WriteLine("UsingTaskFromResult: {0}", DateTime.Now - t);
}
}

输出(重复 10,000/100,000/1000,000 次):

Repeat 10000 times.
UsingAsyncModifier: 00:00:00.1043980
UsingTaskCompletionSource: 00:00:00.0095270
UsingTaskFromResult: 00:00:00.0089460

重复 10,000 次,UsingTaskFromResult 比 UsingAsyncModifier 快 10 倍。

Repeat 100000 times.
UsingAsyncModifier: 00:00:00.1676000
UsingTaskCompletionSource: 00:00:00.0872020
UsingTaskFromResult: 00:00:00.0870180

重复 100,000 次,UsingTaskFromResult 比 UsingAsyncModifier 快 2 倍。

Repeat 1000000 times.
UsingAsyncModifier: 00:00:00.8458490
UsingTaskCompletionSource: 00:00:00.8870980
UsingTaskFromResult: 00:00:00.9027320

重复 1,000,000 次,UsingAsyncModifier 比 UsingTaskFromResult 稍快。

我的想法是,async 修饰符只是创建了一个已完成的任务,就像 Task.FromResult() 那样。但是benchmark并不能证明我的想法。为什么?

最佳答案

虽然我使用 DateTime 看到类似的结果,但使用 Stopwatch 进行时间测量表明使用 UsingAsyncModifier() 的迭代需要 2 次更多的持续时间(比使用 UsingTaskCompletionSource()UsingTaskFromResult(),两者显示相同的近似持续时间)即使有 1 000 000 次迭代

这里是输出:

Repeat 1000000 times.
UsingAsyncModifier: 5458
UsingTaskCompletionSource: 2838
UsingTaskFromResult: 2556

用你的代码使用秒表

class Program
{
public static async Task<int> UsingAsyncModifier()
{
return 10;
}

public static Task<int> UsingTaskCompletionSource()
{
TaskCompletionSource<int> tcs = new TaskCompletionSource<int>();
tcs.SetResult(10);
return tcs.Task;
}
public static Task<int> UsingTaskFromResult()
{
return TaskEx.FromResult(10);
}
static void Main(string[] args)
{
//DateTime t = DateTime.Now;
Stopwatch timer = new Stopwatch();
const int repeat = 1000*1000; // Results volatile while repeat grows.
Console.WriteLine("Repeat {0} times.", repeat);

int j = 0;
//DateTime t = DateTime.Now;
timer.Start();
for (int i = 0; i < repeat; i++)
{
j += UsingAsyncModifier().Result;
}
timer.Stop();
Console.WriteLine("UsingAsyncModifier: {0}"
, timer.ElapsedMilliseconds);
//t = DateTime.Now;
timer.Reset();

j = 0;

timer.Start();
for (int i = 0; i < repeat; i++)
{
j += UsingTaskCompletionSource().Result;
}
timer.Stop();
Console.WriteLine("UsingTaskCompletionSource: {0}"
, timer.ElapsedMilliseconds);
//t = DateTime.Now;
timer.Reset();
j = 0;
timer.Start();
for (int i = 0; i < repeat; i++)
{
j += UsingTaskFromResult().Result;
}
timer.Stop();
Console.WriteLine("UsingTaskFromResult: {0}"
, timer.ElapsedMilliseconds);

Console.ReadLine();
}
}

Stephen Toub 在他的“Async Performance: Understanding the Costs of Async and Await”中解释道:

When working with synchronous code, methods with empty bodies are practically free. This is not the case for asynchronous methods

阅读它了解更多详情

关于c# - "synchronized"异步方法的开销是多少?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16662229/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com