Async streams with IAsyncEnumerable in C Sharp (C#) - Time & Space Complexity
When using async streams with IAsyncEnumerable, it's important to understand how the time to process items grows as the number of items increases.
We want to know how the total work changes as more data is streamed asynchronously.
Analyze the time complexity of the following code snippet.
public async IAsyncEnumerable<int> GenerateNumbersAsync(int count)
{
for (int i = 0; i < count; i++)
{
await Task.Delay(10); // Simulate async work
yield return i;
}
}
public async Task ProcessNumbersAsync(int count)
{
await foreach (var number in GenerateNumbersAsync(count))
{
Console.WriteLine(number);
}
}
This code asynchronously generates numbers one by one, simulating a delay, and processes each number as it arrives.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The
forloop insideGenerateNumbersAsyncthat runscounttimes. - How many times: Exactly once per item, so
counttimes.
Each item causes one loop iteration with a small async delay and a yield. As the number of items grows, the total time grows proportionally.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 async yields and delays |
| 100 | 100 async yields and delays |
| 1000 | 1000 async yields and delays |
Pattern observation: The total work grows linearly as the number of items increases.
Time Complexity: O(n)
This means the time to process all items grows directly in proportion to the number of items streamed.
[X] Wrong: "Because the method is async, it runs instantly regardless of input size."
[OK] Correct: Async means the method can pause and resume, but it still processes each item one by one, so total time depends on how many items there are.
Understanding async streams helps you explain how programs handle data that arrives over time, a useful skill when working with real-world data sources like files, networks, or user input.
"What if we removed the await Task.Delay(10) inside the loop? How would the time complexity change?"