有两种代码变体,其中秒表以不同的方式测量经过的时间。我不明白为什么。这是代码 -
1.第一个例子(在AsyncAction方法中,同步部分在前,然后是异步部分)-
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
namespace DifferentTasks
{
public class SyncAndAsyncPartsOfMethod
{
public async Task Execute()
{
IEnumerable<int> data = Enumerable.Range(0, 100);
Stopwatch stopwatch = Stopwatch.StartNew();
Task t = Task.WhenAll(data.Select(AsyncAction));
await t;
stopwatch.Stop();
double elapsedSeconds = TimeSpan.FromMilliseconds(stopwatch.ElapsedMilliseconds).TotalSeconds;
Console.WriteLine("Elapsed seconds overall - " + elapsedSeconds);
}
private async Task AsyncAction(int x)
{
Stopwatch stopwatch = Stopwatch.StartNew();
Thread.Sleep(TimeSpan.FromSeconds(1));
await Task.Delay(TimeSpan.FromSeconds(1));
stopwatch.Stop();
double elapsedSeconds = TimeSpan.FromMilliseconds(stopwatch.ElapsedMilliseconds).TotalSeconds;
Console.WriteLine($"AsyncAction {x} is finished, elapsed seconds - {elapsedSeconds}, thread - {Thread.CurrentThread.ManagedThreadId}");
}
}
}
结论:总经过秒数 - 102 秒
AsyncAction 方法中的秒表显示该方法每次运行正好两秒:
AsyncAction 95 is finished, elapsed seconds - 2,046, thread - 5
AsyncAction 96 is finished, elapsed seconds - 2,032, thread - 4
AsyncAction 97 is finished, elapsed seconds - 2,015, thread - 4
AsyncAction 98 is finished, elapsed seconds - 2,029, thread - 4
AsyncAction 99 is finished, elapsed seconds - 2,046, thread - 5
2.第二个例子(在AsyncAction方法中,异步部分在前,然后是同步部分)-
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
namespace DifferentTasks
{
public class SyncAndAsyncPartsOfMethod
{
public async Task Execute()
{
IEnumerable<int> data = Enumerable.Range(0, 100);
Stopwatch stopwatch = Stopwatch.StartNew();
Task t = Task.WhenAll(data.Select(AsyncAction));
await t;
stopwatch.Stop();
double elapsedSeconds = TimeSpan.FromMilliseconds(stopwatch.ElapsedMilliseconds).TotalSeconds;
Console.WriteLine("Elapsed seconds overall - " + elapsedSeconds);
}
private async Task AsyncAction(int x)
{
Stopwatch stopwatch = Stopwatch.StartNew();
await Task.Delay(TimeSpan.FromSeconds(1));
Thread.Sleep(TimeSpan.FromSeconds(1));
stopwatch.Stop();
double elapsedSeconds = TimeSpan.FromMilliseconds(stopwatch.ElapsedMilliseconds).TotalSeconds;
Console.WriteLine($"AsyncAction {x} is finished, elapsed seconds - {elapsedSeconds}, thread - {Thread.CurrentThread.ManagedThreadId}");
}
}
}
结论:总经过秒数 - 13 秒
AsyncAction 方法中的秒表现在显示 AsyncAction 方法每次执行所需的时间更长 -
AsyncAction 92 is finished, elapsed seconds - 3,066, thread - 6
AsyncAction 89 is finished, elapsed seconds - 4,035, thread - 8
AsyncAction 87 is finished, elapsed seconds - 4,082, thread - 6
AsyncAction 85 is finished, elapsed seconds - 4,082, thread - 5
AsyncAction 83 is finished, elapsed seconds - 5,051, thread - 8
AsyncAction 81 is finished, elapsed seconds - 5,067, thread - 4
启动这些课程的代码 -
using System;
using System.Runtime.InteropServices;
namespace DifferentTasks
{
internal class Program
{
public static void Main(string[] args)
{
SyncAndAsyncPartsOfMethod syncAndAsyncPartsOfMethod = new();
syncAndAsyncPartsOfMethod.Execute().Wait();
Console.ReadLine();
}
}
}
我想了解为什么 -
- 根据是先执行异步部分还是先执行同步部分,总运行时间会有所不同
- 为什么先执行异步部分,方法的执行时间每次都会增加。