Not every async is good
Recently I found code that looks like this:
static async Task Main(string[] args)
{
const int MaxLines = 10000;
string line;
List<string> lines = new List<string>();
Stopwatch stopwatch = Stopwatch.StartNew();
using var reader = new StreamReader(args[0]);
while ((line = await reader.ReadLineAsync()) != null)
{
if (lines.Count >= MaxLines)
{
lines.Clear();
}
lines.Add(line);
}
Console.WriteLine(stopwatch.ElapsedMilliseconds);
}
As you can see this code is written “by the book”, everything is async: Main function is async, reading from file is async, and looks like this code is good. And if you ask people around which version would be faster: this one or non-async you will get mixed result. Some will say that async version will be faster,
[...Read More]