public class SpanBenchmark
{
const int times = 10000;
[Benchmark]
public async Task TaskTest1()
{
var v = await TaskTest(10);
}
[Benchmark]
public async Task TaskTest2()
{
var v = TaskTest(10);
if (!v.IsCompleted)
await v;
}
[Benchmark]
public async ValueTask TaskTest3()
{
var v = await ValueTaskTest(10);
}
[Benchmark]
public async ValueTask TaskTest4()
{
var v = ValueTaskTest(10);
if (!v.IsCompleted)
await v;
}
[Benchmark]
public async Task TaskTest5()
{
var v = await ValueTaskTest(10);
}
[Benchmark]
public async Task TaskTest6()
{
var v = ValueTaskTest(10);
if (!v.IsCompleted)
await v;
}
private ValueTask<int> ValueTaskTest(int i)
{
return new ValueTask<int>(i);
}
private Task<int> TaskTest(int i)
{
return Task.FromResult(i);
}
}
结论:await越少性能越好