You can use dispatch_apply with Dispatch Global Queue to parallelize it, but your code seems to be less efficient at the same time. Because the storage object requires exclusive access and is used tightly by the unit, this way it will cause a gigantic lock for the battery object.
For example, this code is almost non-competitive, although when using dispatch_apply with Dispatch Global Queue.
dispatch_semaphore_t sema = dispatch_semaphore_create(1); dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0); dispatch_apply([array count], queue, ^(size_t index) { dispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER); acc = block(acc, [array objectAtIndex:index]); dispatch_semaphore_signal(sema); }); dispatch_release(sema);
For efficient parallelization, you need to separate the block and the implementation of the battery.
Edition:
(I did not check your code algorithm.)
dispatch_queue_t result_queue = dispatch_queue_create(NULL, NULL);
You are using a sequential queue. A sequential queue executes one block at a time. So it could be
dispatch_queue_t result_queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
or
dispatch_queue_t result_queue = dispatch_queue_create(NULL, DISPATCH_QUEUE_CONCURRENT);
source share