我编写了一个非常简单的应用程序,它使用Fibonacci函数来比较TPL Parallel.ForEach 和PPL parallel_for_each,结果非常奇怪,在具有8个内核的PC上,c#比 c ++ 快11秒.
vs2010和2011预览都有相同的结果.
C#代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.Concurrent;
using System.Threading.Tasks;
using System.Diagnostics;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
var ll = new ConcurrentQueue<Tuple<int, int>>();
var a = new int[12] { 40, 41, 42, 43, 44, 45, 46, 47, 35, 25, 36, 37 };
long elapsed = time_call(() =>
{
Parallel.ForEach(a, (n) => { ll.Enqueue(new Tuple<int, int>(n, fibonacci(n))); });
});
Console.WriteLine("TPL C# …Run Code Online (Sandbox Code Playgroud)