title: How can doing tasks in multiple threads be 100 times slower than doing sequentially on the main thread?
body: <p>I have this <a href="https://stackoverflow.com/questions/23505636/transforming-a-code-to-grand-central-dispatch#23505636">other question of mine</a> where I have asked about converting a code from sequential to parallel processing using Grand Central Dispatch.</p>
  
  <p>I will copy the question text to makes things easy...</p>
  
  <hr>
  
  <p>I have an array of NSNumbers that have to pass thru 20 tests. If one test fails than the array is invalid if all tests pass than the array is valid. I am trying to do it in a way that as soon as the first failure happens it stops doing the remaining tests. If a failure happens on the 3rd test then stop evaluating other tests.</p>
  
  <p>Every individual test returns YES when it fails and NO when it is ok.</p>
  
  <p>I am trying to convert the code I have that is serial processing, to parallel processing with grand central dispatch, but I cannot wrap my head around it.</p>
  
  <p>This is what I have.</p>
  
  <p>First the definition of the tests to be done. This array is used to run the tests.</p>
  
  <pre><code>#define TESTS  @[         \
      @"averageNotOK:",     \
      @"numbersOverRange:", \
      @"numbersUnderRange:",\
      @"numbersForbidden:", \
      // ... etc etc
      @"numbersNotOnCurve:"]
  
  
  - (BOOL) numbersPassedAllTests:(NSArray *)numbers {
  
    NSInteger count = [TESTS count];
  
    for (int i=0; i&lt;count; i++) {
  
      NSString *aMethodName = TESTS[i];
  
          SEL selector = NSSelectorFromString(aMethodName);
  
          BOOL failed = NO;
  
          NSMethodSignature *signature = [[self class] instanceMethodSignatureForSelector:selector];
  
          NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:signature];
          [invocation setSelector:selector];
          [invocation setTarget:self];
          [invocation setArgument:&amp;numbers atIndex:2];
          [invocation invoke];
  
  
          [invocation getReturnValue:&amp;failed];
  
          if (failed) {
            return NO;
          }
    }
    return YES;
  
  }
  </code></pre>
  
  <p>This work perfectly but performs the tests sequentially.</p>
  
  <p>After working on the code with the help of an user, I got this code using grand central dispatch:</p>
  
  <pre><code>- (BOOL) numbersPassedAllTests:(NSArray *)numbers {
  
    volatile __block int32_t hasFailed = 0;
  
    NSInteger count = [TESTS count];
  
    __block NSArray *numb = [[NSArray alloc] initWithArray:numbers];
  
    dispatch_apply(
                   count,
                   dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0),
                   ^(size_t index)
                   {
                     // do no computation if somebody else already failed
                     if(hasFailed) {
                      return;
                     }
  
  
                     SEL selector = NSSelectorFromString(TESTS[index]);
  
                     BOOL failed = NO;
  
                     NSMethodSignature *signature = [[self class] instanceMethodSignatureForSelector:selector];
  
                     NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:signature];
                     [invocation setSelector:selector];
                     [invocation setTarget:self];
                     [invocation setArgument:&amp;numb atIndex:2];
                     [invocation invoke];
  
                     [invocation getReturnValue:&amp;failed];
  
                     if(failed)
                       OSAtomicIncrement32(&amp;hasFailed);
                   });
  
    return !hasFailed;
  }
  </code></pre>
  
  <p>Activity Monitor shows what appears to be the cores being used with more intensity but this code is at least 100 times slower than the older one working sequentially!</p>
  
  <p>How can that be?</p>