Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9120277
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T05:35:27+00:00 2026-06-17T05:35:27+00:00

I try to learn Grand Central Dispatch (GCD) and use the following code to

  • 0

I try to learn Grand Central Dispatch (GCD) and use the following code to test:

With 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;
}

Without 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;
}

The test result with GCD:

$ time ./testgcd 100000000 10000000
4.254 secs

The test without GCD:

$ time ./nogcd 100000000 10000000
1.462 secs

I thought that GCD should reduce execution time, but the results show the opposite.
I am not sure if I misuse GCD.
The OS environment is Mac OS X 10.8 with Xcode 4.5. The compiler is Clang++ 3.1.
The hardware is Macbook Pro with i5 CPU, which has two cores.

For comparison, I use OpenMP (also using GCC shipped with Xcode 4.5 on the same laptop):

#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;
}

and w/wo (-fopenmp), I have two executables to test,

with -fopenmp flag while compiling:

$ time ./testopenmp 100000000
1.280 secs

without -fopenmp flag while compiling:

$ time ./testnoopenmp 100000000
1.626 secs

With OpenMP, the executing time are reduced.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T05:35:28+00:00Added an answer on June 17, 2026 at 5:35 am

    GCD does not necessarily have to increase an execution time. The reason why it does so in your case is because you are doing it wrong. It is important that you know why your application is slow in the first place. So I went and ran your code under multi-core profiler (Instruments.app), and here is what it shows:

    Multi-Core Profiling Screenshot

    As you can see, the graph is mostly yellow. Yellow means that a thread is doing nothing, and waiting for some task to execute. Green means that it executes a task. In other words, the way you have written your code, the application spends 99% of its time passing tasks around, and each task execution takes almost no time — way too much overhead. So why does this happen?

    Because you have scheduled about 100000000 tasks to run. Running each task has some overhead, which is by far greater than assigning an integer to an array. The rule of thumb is not to schedule a task if its complexity is less than that of a inter-thread communication.

    So how do you fix this? Schedule less tasks, do more in each task. For example:

    int main(int argc, char *argv[])
    {
       const int N = atoi(argv[1]);
       __block std::vector<int> a(N, 0);
       dispatch_apply(4,
         dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0),
         ^(size_t iN)
         {
             size_t s = a.size()/4;
             size_t i = (s*iN);
             size_t n = i + s;
             //printf("Iteration #%lu [%lu, %lu]\n", iN, i, n);
             while (i < n) {
                 a[i] = i++;
             }
         });
      return 0;
    }
    

    Now, the profiler shows the following:

    Not that bad

    Run the test again and GCD is a little bit faster:

    $ time ./test_nogcd 100000000 10000000
    
    real    0m0.516s
    user    0m0.378s
    sys 0m0.138s
    $ time ./test_gcd 100000000 10000000
    
    real    0m0.507s
    user    0m0.556s
    sys 0m0.138s
    

    Perhaps running less tasks will make it better? Try it out. With such a simple workflow, chances are that you are much better off using a single-threaded SIMD implementation. Or maybe not 🙂

    Note that you have to take extra care in some situations, for example, when a total size cannot be divided into N equal parts, etc. I have omitted all error checking for simplicity.

    Also, there are tons of nuances when it comes to paralleling tasks on today’s commodity hardware. I’d recommend you get yourself familiar with MESI, false sharing, memory barriers, CPU caches, cache oblivious algorithms, etc. And remember – always use a profiler!

    Hope it helps. Good Luck!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've written a simple test program to try to learn how to use template
I am doing the below test to try and learn more about LINQ to
i try to learn scheme and as a test project i wanted to create
I am following this tutorial http://static.springsource.org/docs/Spring-MVC-step-by-step/part4.html to try and learn Spring. Everything worked fine
I'm trying to learn how to use OpenMP by parallelizing a monte carlo code
I am following along with the music store example to try learn ASP.NET MVC.
I am following a tutorial to learn how to use form based security in
I try to learn how to use the StrCmpLogicalW function. There is a post
I came across the following definition as I try to learn Haskell using a
I was reading the source code of backbone today to try and learn how

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.