我想在一些同步代码中添加并发,发现过程中存在性能问题,很难理解。
下面代码的运行结果是:
Mission Fibonacci1Async cost 9.4195388 seconds, value 75025
Mission Fibonacci2Async cost 0.2260129 seconds, value 75025
唯一不同的是第二个函数添加了一行await Task.WhenAll(new Task[] { t1, t2 }); ,使性能提升40倍。
谁能给我解释一下?
static Task<int> Fibonacci1Async(int n)
{
return Task.Run<int>(() => Fibonacci1(n));
}
static int Fibonacci1(int n)
{
if (n == 0) return 0;
else if (n == 1) return 1;
else
{
var t1 = Fibonacci1Async(n - 1);
var t2 = Fibonacci1Async(n - 2);
return t1.Result + t2.Result;
}
}
static Task<int> Fibonacci2Async(int n)
{
return Task.Run<int>(() => Fibonacci2(n));
}
static int Fibonacci2(int n)
{
if (n == 0) return 0;
else if (n == 1) return 1;
else
{
var t1 = Fibonacci2Async(n - 1);
var t2 = Fibonacci2Async(n - 2);
Task.WaitAll(new Task[] { t1, t2 });
return t1.Result + t2.Result;
}
}
static void Benchmark(Func<int, Task<int>> func)
{
DateTime time = DateTime.Now;
var task = func(25);
task.Wait();
TimeSpan cost = DateTime.Now - time;
Console.WriteLine("Mission {0} cost {1} seconds value {2}", func.Method.Name, cost.TotalSeconds, task.Result);
}
static void Main(string[] args)
{
Benchmark(Fibonacci1Async);
Benchmark(Fibonacci2Async);
Console.ReadKey();
return;
}