Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

ios

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?

like image 911
Michael Ozeryansky Avatar asked Jan 29 '18 08:01

Michael Ozeryansky


1 Answers

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

like image 118
clemens Avatar answered Oct 21 '22 23:10

clemens