gpt4 book ai didi

c# - 测试显示 "await"明显更慢,即使等待的对象已经完成

转载 作者:可可西里 更新时间:2023-11-01 08:28:00 25 4
gpt4 key购买 nike

我想通过使用 await/async 来测试程序的开销。

为了对此进行测试,我编写了以下测试类:

public class Entity : INotifyCompletion {
private Action continuation;
private int i;

public void OnCompleted(Action continuation) {
this.continuation = continuation;
}

public Entity GetAwaiter() {
return this;
}

public Entity GetResult() {
return this;
}

public bool IsCompleted { get { return true; } }

public void Execute() {
if (i > 0) Console.WriteLine("What");
}
}

然后我写了一个测试工具。测试工具通过 TestA 和 TestB 迭代 1600 次,仅测量后者 1500 次(以允许 JIT“预热”)。 set 是 Entity 对象的集合(但实现无关)。该集合中有 50,000 个实体。测试工具使用 Stopwatch 类进行测试。

private static void DoTestA() {
Entity[] objects = set.GetElements();
Parallel.For(0, objects.Length, async i => {
Entity e = objects[i];
if (e == null) return;

(await e).Execute();
});
}

private static void DoTestB() {
Entity[] objects = set.GetElements();
Parallel.For(0, objects.Length, i => {
Entity e = objects[i];
if (e == null) return;

e.Execute();
});
}

这两个例程是相同的,除了一个在调用 Execute() 之前等待实体(Execute() 没有做任何有用的事情,它只是一些愚蠢的代码来确保处理器真的在为每个实体)。


在针对 AnyCPUrelease 模式下执行我的测试后,我得到以下输出:

>>> 1500 repetitions >>> IN NANOSECONDS (1000ns = 0.001ms)
Method Avg. Min. Max. Jitter Total
A 1,301,465ns 1,232,200ns 2,869,000ns 1,567,534ns ! 1952.199ms
B 130,053ns 116,000ns 711,200ns 581,146ns ! 195.081ms

如您所见,带有 await 的方法大约慢了 10 倍。

事实是,据我所知,没有什么可等待的 - GetResult 始终为真。这是否意味着即使等待的“事物”已经准备好,状态机也会执行?

如果是这样,有什么办法解决这个问题吗?我想使用 async/await 的语义,但这种开销对我的应用程序来说太高了......


编辑:请求后添加完整的基准测试代码:

程序.cs

using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace CSharpPerfTest {
public class Entity : INotifyCompletion {
private Action continuation;
private int i;

public void OnCompleted(Action continuation) {
this.continuation = continuation;
}

public Entity GetAwaiter() {
return this;
}

public Entity GetResult() {
return this;
}

public bool IsCompleted { get { return true; } }

public void Execute() {
if (i > 0) Console.WriteLine("What");
}
}

static class Program {
static ConcurrentSet<Entity> set;
const int MAX_ELEMENTS = 50000;

// Called once before all testing begins
private static void OnceBefore() {
set = new ConcurrentSet<Entity>();

Parallel.For(0, MAX_ELEMENTS, i => {
set.Add(new Entity());
});
}

// Called twice each repetition, once before DoTestA and once before DoTestB
private static void PreTest() {

}

private static void DoTestA() {
Entity[] objects = set.GetElements();
Parallel.For(0, objects.Length, async i => {
Entity e = objects[i];
if (e == null) return;
(await e).Execute();
});
}

private static void DoTestB() {
Entity[] objects = set.GetElements();
Parallel.For(0, objects.Length, i => {
Entity e = objects[i];
if (e == null) return;
e.Execute();
});
}

private const int REPETITIONS = 1500;
private const int JIT_WARMUPS = 10;

#region Test Harness
private static double[] aTimes = new double[REPETITIONS];
private static double[] bTimes = new double[REPETITIONS];

private static void Main(string[] args) {
Stopwatch stopwatch = new Stopwatch();

OnceBefore();

for (int i = JIT_WARMUPS * -1; i < REPETITIONS; ++i) {
Console.WriteLine("Starting repetition " + i);

PreTest();
stopwatch.Restart();
DoTestA();
stopwatch.Stop();
if (i >= 0) aTimes[i] = stopwatch.Elapsed.TotalMilliseconds;

PreTest();
stopwatch.Restart();
DoTestB();
stopwatch.Stop();
if (i >= 0) bTimes[i] = stopwatch.Elapsed.TotalMilliseconds;
}

DisplayScores();
}

private static void DisplayScores() {
Console.WriteLine();
Console.WriteLine();

bool inNanos = false;
if (aTimes.Average() < 10 || bTimes.Average() < 10) {
inNanos = true;
for (int i = 0; i < aTimes.Length; ++i) aTimes[i] *= 1000000;
for (int i = 0; i < bTimes.Length; ++i) bTimes[i] *= 1000000;
}

Console.WriteLine(">>> " + REPETITIONS + " repetitions >>> " + (inNanos ? "IN NANOSECONDS (1000ns = 0.001ms)" : "IN MILLISECONDS (1000ms = 1s)"));
Console.WriteLine("Method Avg. Min. Max. Jitter Total");

Console.WriteLine(
"A "
+ (String.Format("{0:N0}", (long) aTimes.Average()) + (inNanos ? "ns" : "ms")).PadRight(13, ' ')
+ (String.Format("{0:N0}", (long) aTimes.Min()) + (inNanos ? "ns" : "ms")).PadRight(13, ' ')
+ (String.Format("{0:N0}", (long) aTimes.Max()) + (inNanos ? "ns" : "ms")).PadRight(13, ' ')
+ (String.Format("{0:N0}", (long) Math.Max(aTimes.Average() - aTimes.Min(), aTimes.Max() - aTimes.Average())) + (inNanos ? "ns" : "ms")).PadRight(13, ' ')
+ ((long) aTimes.Sum() >= 10000 && inNanos ? "! " + String.Format("{0:f3}", aTimes.Sum() / 1000000) + "ms" : (long) aTimes.Sum() + (inNanos ? "ns" : "ms"))
);
Console.WriteLine(
"B "
+ (String.Format("{0:N0}", (long) bTimes.Average()) + (inNanos ? "ns" : "ms")).PadRight(13, ' ')
+ (String.Format("{0:N0}", (long) bTimes.Min()) + (inNanos ? "ns" : "ms")).PadRight(13, ' ')
+ (String.Format("{0:N0}", (long) bTimes.Max()) + (inNanos ? "ns" : "ms")).PadRight(13, ' ')
+ (String.Format("{0:N0}", (long) Math.Max(bTimes.Average() - bTimes.Min(), bTimes.Max() - bTimes.Average())) + (inNanos ? "ns" : "ms")).PadRight(13, ' ')
+ ((long) bTimes.Sum() >= 10000 && inNanos ? "! " + String.Format("{0:f3}", bTimes.Sum() / 1000000) + "ms" : (long) bTimes.Sum() + (inNanos ? "ns" : "ms"))
);

Console.ReadKey();
}
#endregion

}
}

最佳答案

如果你的函数has a response time that 1ms for 50,000 calls is considered significant您不应该等待该代码,而应该同步运行它。

使用异步代码的开销很小,它必须为内部驱动它的状态机添加一个函数调用。如果与运行状态机的开销成本相比,您进行异步的工作也很小,那么您应该编写需要重新考虑代码是否应该异步的代码。

关于c# - 测试显示 "await"明显更慢,即使等待的对象已经完成,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22870840/

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