Reputation: 309
Given this:
private static async Task<int> Operation1()
{
await Task.Delay(1000);
return 1;
}
private static async Task<int> Operation2()
{
await Task.Delay(1000);
return 1;
}
this is running sequentially, i.e. it takes 2 seconds (measured using Stopwatch
)
var a = await Operation1();
var b = await Operation2();
var result = Operation3(a, b);
while this takes only 1 second, running things in parallel:
var taskA = Operation1();
var taskB = Operation2();
var a = await taskA;
var b = await taskB;
var result = Operation3(a, b);
Why is that? I would totally assume those 2 are equivalent and produce the same IL.
Upvotes: 0
Views: 277
Reputation: 43683
The first piece of code is equivalent to this:
var taskA = Operation1();
var a = await taskA;
// At this point the taskA is completed
var taskB = Operation2();
var b = await taskB;
Now it should be obvious why it's not equivalent with the second version:
var taskA = Operation1();
var taskB = Operation2();
// At this point both tasks are started, and probably none is completed
var a = await taskA;
var b = await taskB;
As a side note, one of these two approaches demonstrate a bad practice. Can you guess if it's the first or the second approach? You can find the answer to this quiz in the 1st revision of this answer.
Upvotes: 1
Reputation: 121
In the first example you start Operation1
and wait until it is finished, then you start Operation2
.
In the second example, you start Operation1
and then Operation2
, after which you wait for Operation1
and then Operation2
.
So Operation1
and Operation2
run in parallel in the 2nd example and sequentially in the first.
Upvotes: 1
Reputation: 4666
This variants absolutely not the same.
The first one is sequential - you start Operation1 and wait it to finish and then run Operation2. Yes, the operations may be executed in separate threads (and may be on the same, as it is depends on thread pool implementation detail), but synchronisation/awaits makes the control flow sequential.
The second is parallel - you starts two operations independently in parallel, then wait for completion. So, here the both operations working in the same time (and again, they may be executed sequentially depending on the thread pool implementation and your actual work implementation (Task.Delay in your sample))
Upvotes: 0
Reputation: 89246
In the first case Operation2()
doesn't run and start a task until the task returned from Operation1()
completes.
In the second case both Operation1()
and Operation2()
run, creating and starting their tasks before you await
completion of the task returned from Operation1()
Upvotes: 4