Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

fastest way to execute multiple methods all at the same time

I have 4 methods, each method takes a second or two before the method returns, these methods return UIImage, I need these images to display the fastest way I can.

-(NSMutableArray*)prepareImages{
  UIImage *imageToModifyUsingUIImageCategory;

  NSMutableArray *imageArray = [[NSMutableArray alloc]initWithObjects:
  [imageToModifyUsingUIImageCategory doSomethingAndReturn1],
  [imageToModifyUsingUIImageCategory doSomethingAndReturn2],
  [imageToModifyUsingUIImageCategory doSomethingAndReturn3],
  [imageToModifyUsingUIImageCategory doSomethingAndReturn4],nil];    
  return imageArray;
}

at the end of the method above, I will have 4 images from that array. each "doSomethingAndReturn" method takes a second or two, means my prepareImages method will finish execution approximately 5 seconds. too long huh?

my QUESTION is, what could be the other way to accomplish all these faster? is GCD an option for me? how?

Any help would be much appreciated. thanks!

like image 367
janusfidel Avatar asked Jun 01 '12 09:06

janusfidel


3 Answers

Assuming you don't mind loading the images sequentially in the background (rather than in parallel), a simple dispatch to a background queue to run prepareImages will do the trick:

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_LOW, 0), ^{
    NSArray *images = [self prepareImages];

    // now send the result back to the main thread so we can do
    // UIKit stuff
    dispatch_async(dispatch_get_main_queue(), ^{
        // set the images on your UIImageViews here...
    });
});

This will load the images in a low priority background queue, then dispatch back to the main thread once it is done.

If you want to keep this even lower priority, you can use the DISPATCH_QUEUE_PRIORITY_BACKGROUND, although this is only available in iOS 4.3 and later. You should check the return value from dispatch_get_global_queue in this case and if it returns 0 you can fallback on one of the other priorities.

If you want to load each image in parallel, you should probably turn your doSomethingAndReturn methods into NSOperation subclasses and use an NSOperationQueue to run them all. That would require a bit more effort to implement. Also beware of memory usage if processing multiple large images at once.

like image 68
Mike Weller Avatar answered Oct 17 '22 00:10

Mike Weller


you could use a NSOperationQueue:

The NSOperationQueue class regulates the execution of a set of NSOperation objects. After being added to a queue, an operation remains in that queue until it is explicitly canceled or finishes executing its task. Operations within the queue (but not yet executing) are themselves organized according to priority levels and inter-operation object dependencies and are executed accordingly. An application may create multiple operation queues and submit operations to any of them.

example for a NSOperationQueue imeplementation

or a GCD Queue:

Grand Central Dispatch (GCD) dispatch queues are a powerful tool for performing tasks. Dispatch queues let you execute arbitrary blocks of code either asynchronously or synchronously with respect to the caller. You can use dispatch queues to perform nearly all of the tasks that you used to perform on separate threads. The advantage of dispatch queues is that they are simpler to use and much more efficient at executing those tasks than the corresponding threaded code.

example for a GCD Queue implementation

like image 39
CarlJ Avatar answered Oct 17 '22 00:10

CarlJ


You can execute in same time on another core of your processor for do that try:

NSThread detachNewThreadSelector:@selector(YOUR_METHODS_HERE:) toTarget:self withObject:nil];
NSThread detachNewThreadSelector:@selector(YOUR_METHODS_HERE:) toTarget:self withObject:nil];
NSThread detachNewThreadSelector:@selector(YOUR_METHODS_HERE:) toTarget:self withObject:nil];
NSThread detachNewThreadSelector:@selector(YOUR_METHODS_HERE:) toTarget:self withObject:nil];

If your processor has four core each method will be perform on a different core.

like image 1
JMBise Avatar answered Oct 17 '22 00:10

JMBise