9
I have this code and as you can see I created two examples, Parallel and Notparallel.
I expected both to return me 3000ms, because both should run async (2000 and 3000) and the total time would be 3000, but Nonparallel, is taking 5000ms, where it is the sum of the two, it seems that one waits for the other to finish.. even though you’re async.
static class Program
{
static async Task<string> GetTaskAsync(int timeout)
{
Console.WriteLine("Task Thread: " + Thread.CurrentThread.ManagedThreadId);
await Task.Delay(timeout);
return timeout.ToString();
}
static async Task Main()
{
Console.WriteLine("Main Thread: " + Thread.CurrentThread.ManagedThreadId);
Console.WriteLine("Should be greater than 5000");
await Watch(NotParallel);
Console.WriteLine("Should be less than 5000");
await Watch(Parallel);
}
public static async Task Parallel()
{
var res1 = GetTaskAsync(2000);
var res2 = GetTaskAsync(3000);
Console.WriteLine("result: " + await res1 + await res2);
}
public static async Task NotParallel()
{
var res1 = await GetTaskAsync(2000);
var res2 = await GetTaskAsync(3000);
Console.WriteLine("result: " + res1 + res2);
}
private static async Task Watch(Func<Task> func) {
var sw = new Stopwatch();
sw.Start();
await func?.Invoke();
sw.Stop();
Console.WriteLine("Elapsed: " + sw.ElapsedMilliseconds);
Console.WriteLine("---------------");
}
}
Example of the Result:
You want to explain how
Parallel()
andNotParallel()
are different?– ramaral
Give more details of what happens, and what is waiting to happen. see https://answall.com/q/1946/101 to understand that parallelizing does not guarantee better performance, in fact the performance is even worse, only ends earlier in cases that is correct, Maybe this already answers: https://answall.com/q/201998/101 It may also be useful: https://answall.com/q/175304/101 and https://answall.com/a/157440/101 Finally, how do you know that it is in parallel? I don’t think you are: https://answall.com/q/166032/101
– Maniero
@Maniero I am wrong to say that the two methods will have an identical behavior and that one cannot assume that one "is parallel" and the other " is unparalleled"? Ops didn’t see that you edited the comment.
– ramaral
@ramaral, dont see the Names of the methods.
– alerya
@alerya You’re being sarcastic, right?(You’re being sarcastic, right?) :)
– ramaral
I will edit here and put the result @Maniero to illustrate better!
– Thiago Loureiro
@exactly, wanted to understand why the behavior of the two are different?
– Thiago Loureiro
Yeppers - Why one is Faster than other ?
– alerya
@ramaral the alerya is a friend of Ukrania and we are discussing this subject, and decided to post here on PT tb. We work here in the same country.
– Thiago Loureiro
Now that I’ve seen the results, I understand what you want to ask. Rephrase the question because it gives (me) to understand that you expected very different results but obtained close results.
– ramaral
@ramaral edited there, if you feel comfortable, can edit as well and put something that has in mind in the question to help me :)
– Thiago Loureiro
Now I think you are well and I understand the reason for the question. Seeing this code, without seeing the result, I would say that the two methods would have an approximate execution time.
– ramaral
I can’t answer now, I never fully understood about Async, but I found the question interesting, today I don’t have time to stop and think, test, so I hope someone answers, or I’ll try to see next week, you can charge me. And in fact now you can understand the real question.
– Maniero
@Maniero thanks for the feedback, really me and the alerya spent a while and decided to ask the gurus here :)
– Thiago Loureiro
@ramaral need to analyze better ;)
– Maniero