Grand Central Dispatch (GCD) を学習し、次のコードを使用してテストします。
GCD の場合:
#include <dispatch/dispatch.h>
#include <vector>
#include <cstdlib>
#include <iostream>
int main(int argc, char *argv[])  
{
   const int N = atoi(argv[1]);
   __block std::vector<int> a(N, 0);
   dispatch_apply(N, 
     dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), 
     ^(size_t i) 
     { 
       a[i] = i;
#ifdef DEBUG           
       if ( i % atoi(argv[2]) == 0)
         std::cout << a[i] <<  std::endl;
#endif
     });
  return 0;
}
GCD なし:
#include <vector>
#include <cstdlib>
#include <iostream> 
int main(int argc, char *argv[]) 
{
  const int N = atoi(argv[1]);
  std::vector<int> a(N, 0);
  for (int i = 0; i < N; i++)   
    {
      a[i] = i;
#ifdef DEBUG
      if ( i % atoi(argv[2]) == 0)
    std::cout << a[i] <<  std::endl;
#endif
    }
 return 0;
}
GCD でのテスト結果:
$ time ./testgcd 100000000 10000000
4.254 secs
GCD なしのテスト:
$ time ./nogcd 100000000 10000000
1.462 secs
GCD は実行時間を短縮するはずだと思っていましたが、結果は逆です。GCD を誤用しているかどうかはわかりません。OS環境はMac OS X 10.8 with Xcode 4.5です。コンパイラは Clang++ 3.1 です。ハードウェアは2コアのi5 CPUを搭載したMacbook Pro。
比較のために、OpenMP を使用します (同じラップトップで Xcode 4.5 に同梱されている GCC も使用します)。
#include <vector> 
#include <cstdlib>
int main(int argc, char *argv[])  
{
  const int N = atoi(argv[1]);
  std::vector <int> a(N, 0);
  #pragma omp parallel for
  for (int i = 0; i < N; i++)
    a[i] = i;
  return 0;
}
そして w/wo (-fopenmp)、テストする実行可能ファイルが 2 つあります。
-fopenmpコンパイル中のフラグ付き:
$ time ./testopenmp 100000000
1.280 secs
-fopenmpコンパイル中のフラグなし:
$ time ./testnoopenmp 100000000
1.626 secs
OpenMP を使用すると、実行時間が短縮されます。

