Skip to content

Latest commit

 

History

History
53 lines (52 loc) · 1.34 KB

20181213.ValueTask测试.md

File metadata and controls

53 lines (52 loc) · 1.34 KB
    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);
        }
    }

img

结论:await越少性能越好