Monday, May 7, 2018

What is the overhead of running an empty block on a queue

Leave a Comment

I realized that I was queuing a lot of blocks calling to empty methods. In the debugger it looks like a lot is happening when really all the blocks are empty.

Is there any real performance impact from having empty blocks?

1 Answers

Answers 1

The overhead should be negligible: You may check this with Instruments and a simple program like:

#import <Foundation/Foundation.h>  int main(int argc, const char * argv[]) {     @autoreleasepool {         dispatch_queue_t q = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0);         void (^b)(void) = ^{ };         double d = 2.0;          for(int i = 0; i < 10000000; ++i) {             dispatch_sync(q, b);             d = d * 1.5 - 1.0;         }         NSLog(@"d = %.3f", d);     }     return 0; } 

As you can see in the Instruments stack trace the calls require 40ms for 10 millions synchronous invocations of an empty block. That's not much overhead.

enter image description here

If You Enjoyed This, Take 5 Seconds To Share It

0 comments:

Post a Comment