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

  • Home
  • SEARCH
  • 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 3991234
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T06:35:28+00:00 2026-05-20T06:35:28+00:00

As you may remember, I am trying to use GCD to speed up some

  • 0

As you may remember, I am trying to use GCD to speed up some of my code, namely a collision detection and resolution engine. However, I am clearly doing something wrong because all of my GCD code is significantly slower and less consistent than my serial code (between 1.4x and 10x slower). Allow me to give you an example: I am iterating over an array in a bubble-sort fashion to determine all possible collisions among objects in that array:

- (double) detectCollisionsInArray:(NSArray*)objects
{   
    int count = [objects count];
    if (count > 0)
    {       
        double time = CFAbsoluteTimeGetCurrent();
        for (int i = 0; i < count; i++)
        {
            for (int j = i + 1; j < count; j++)
            {
                /** LOTS AND LOTS OF WORK FOR EACH OBJECT **/
            }
        }

        return CFAbsoluteTimeGetCurrent() - time;
    }

    return 0;
}

Pretty straightforward, and it seems to perform well given the constraints of the problem. However, I would like to take advantage of the fact that the state of each object is not modified in the code section and use GCD to parallelize this work. To do this I am trying something like this:

- (double) detectCollisionsInArray:(NSArray*)objects
{   
    int count = [objects count];
    if (count > 0)
    {
        NSOperationQueue* opQueue = [[NSOperationQueue alloc] init];
        NSBlockOperation* blockOperation = nil;

        double time = CFAbsoluteTimeGetCurrent();
        for (int i = 0; i < count; i++)
        {
            for (int j = i + 1; j < count; j++)
            {
                void (^workBlock) (void) = ^() 
                {
                    /** LOTS AND LOTS OF WORK FOR EACH OBJECT **/
                };

                if (!blockOperation)
                {
                    blockOperation = [NSBlockOperation blockOperationWithBlock:b];
                }
                else
                {
                    [blockOperation addExecutionBlock:workBlock];
                }
            }
        }

        [opQueue addOperation:blockOperation];
        [opQueue autorelease];

        return CFAbsoluteTimeGetCurrent() - time;
    }

    return 0;
}

Can anyone help to put me on the right track and perhaps provide a link to a good GCD tutorial? I have looked over several GCD tutorials and scoured all of the documentation and I still feel that my grasp on the subject is tenuous at best. Thanks!

  • 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-05-20T06:35:28+00:00Added an answer on May 20, 2026 at 6:35 am

    Is there a reason you’re not using the GCD C API and the dispatch_* family of functions? You don’t have much control over the GCD aspects of NSOperationQueue (like which queue you want to submit the blocks to). Also, I can’t tell if you’re using iOS or not, but NSOperationQueue does not use GCD on iOS. That might be the reason it spawned so many threads. Either way, your code will be shorter and simpler if you use the GCD API directly:

    - (double) detectCollisionsInArray:(NSArray*)objects
    {   
      int count = [objects count];
      if (count > 0)
      {
        double time = CFAbsoluteTimeGetCurrent();
    
        dispatch_group_t group = dispatch_group_create();
        dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
        for (int i = 0; i < count; i++)
        {
          dispatch_group_async(group, queue, ^{
            for (int j = i + 1; j < count; j++)
            {
              dispatch_group_async(group, queue, ^{
                /** LOTS AND LOTS OF WORK FOR EACH OBJECT **/
              });
            }
          });
        }
        dispatch_group_wait(group, DISPATCH_TIME_FOREVER);
        dispatch_release(group);
        return CFAbsoluteTimeGetCurrent() - time;
      }
      return 0;
    }
    

    You can use a dispatch_group to group all of the executions together and wait for them all to finish with dispatch_group_wait. If you don’t care to know when the the blocks finish, you can ignore the group part and just use dispatch_async. The dispatch_get_global_queue function will get one of the 3 concurrent queues (low, default or high priority) for you to submit your blocks to. You shouldn’t have to worry about limiting the thread count or anything like that. The GCD scheduler is supposed to do all of that for you. Just make sure you submit to a concurrent queue, which could either be one of the 3 global queues, or a queue you’ve created by passing DISPATCH_QUEUE_CONCURRENT to dispatch_queue_create (this is available starting OS X 10.7 and iOS 5.0).

    If you’re doing some file I/O in each block or taxing some other resource, you might need to reign in GCD and limit the number of blocks you’re submitting to the queue at once. This will have the same effect as limiting the concurrent operation count in an NSOperationQueue. You can use a GCD semaphore to do this:

    - (double) detectCollisionsInArray:(NSArray*)objects
    {   
      int count = [objects count];
      if (count > 0)
      {
        double time = CFAbsoluteTimeGetCurrent();
    
        dispatch_group_t group = dispatch_group_create();
        dispatch_semaphore_t semaphore = dispatch_semaphore_create(10);
        dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
        for (int i = 0; i < count; i++)
        {
          dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
          dispatch_group_async(group, queue, ^{
            for (int j = i + 1; j < count; j++)
            {
              dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
              dispatch_group_async(group, queue, ^{
                /** LOTS AND LOTS OF WORK FOR EACH OBJECT **/
                dispatch_semaphore_signal(semaphore);
              });
            }
            dispatch_semaphore_signal(semaphore);
          });
        }
        dispatch_group_wait(group, DISPATCH_TIME_FOREVER);
        dispatch_release(group);
        dispatch_release(semaphore);
        return CFAbsoluteTimeGetCurrent() - time;
      }
      return 0;
    }
    

    Once you get the hang of it, GCD is very simple to use. I use it all over my code now.

    Can anyone help to put me on the right track and perhaps provide a link to a good GCD tutorial?

    Run, don’t walk over to Mike Ash’s blog. His series on GCD is the clearest and most concise I’ve seen, and it’ll only take you around 30 minutes to read the whole thing. Apple’s WWDC videos from 2010 on GCD And blocks are also pretty good.

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

Sidebar

Related Questions

You may remember these drawings from when you were a child, but now it's
I may be crazy, but I thought I remember reading that OnLoginError produces a
I may be completely off on this, but I swear I remember reading somewhere
I was listening to a podcast recently (may have been SO - can't remember)
This has / may have been asked before, but, as far as I remember,
may i know how to use this getMicrophone with simpleFLVWriter to create flv with
May be some body already used some open source component, writing on jquery. And
In my users controller I am trying to put in place the remember me
i remember there being a way of marking a section of code in eclipse
...as someone may remember, I'm still stuck on C++ strings. Ok, I can write

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.