I have a method in my iPhone app that is very repetitive (called over and over and over sequentially). The method is quite bulky and takes about one second to complete.
我在我的iPhone应用程序中有一个非常重复的方法(不断地按顺序调用)。这种方法非常笨重,大约需要一秒钟就能完成。
My question is this: If I was to muti-thread it to run, say 5 method calls on different threads simultaneously, would that be any faster than running 5 calls one after another? I know of desktop machines with multi-core processors this would be an advantage, but I am not sure about the iPhone.
我的问题是:如果我让它多线程运行,比如在不同的线程上同时运行5个方法调用,这会比一个接一个地运行5个调用快吗?我知道台式电脑有多核处理器,这将是一个优势,但我不确定iPhone。
Any advice?
任何建议吗?
3 个解决方案
#1
2
Use Grand Central Dispatch (aka GCD and libdispatch) and it'll just Do The Right Thing(tm).
使用Grand Central Dispatch(即GCD和libdispatch),它会做正确的事情(tm)。
If you call dispatch_async on one of the global queues, GCD will figure depending on system workload whether to spawn new threads to handle the operations you submit, or whether to run them in series on one thread.
如果您在一个全局队列上调用dispatch_async, GCD将根据系统工作负载确定是生成新的线程来处理您提交的操作,还是在一个线程上以串行方式运行它们。
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
// Do your calculations here
// If you want to run code on the main loop with the result of the calculations,
// do something like this
dispatch_async(dispatch_get_main_queue(), ^{
// Code to run on the main thread here
});
});
You could also use dispatch groups to run some code after all five calls have completed.
您还可以使用分派组在所有五个调用完成之后运行一些代码。
Take a look at the Concurrency Programming Guide and the GCD reference manual for details.
详细信息请参阅并发编程指南和GCD参考手册。
Update
更新
Also look at this article from Ars on GCD. They explain some of the details of how GCD decides how to schedule blocks.
还可以看看这篇来自GCD上Ars的文章。他们解释了GCD如何决定如何安排block的一些细节。
#2
2
If it's something like a network request, it will most probably be faster because you're spending time waiting without really using the processor (although spawning threads is not really the best way to deal with that). But if the method is doing some heavy calculations you won't gain anything. Still, to keep the UI responsive you would run lengthy tasks in the background. It's usually better and simpler to use NSOperation
for such things, so have a look at that class.
如果它是类似于网络请求的东西,那么它很可能会更快,因为您在没有真正使用处理器的情况下花费时间等待(尽管生成线程并不是真正最好的处理方法)。但是如果这个方法做了一些繁重的计算,你不会得到任何东西。不过,为了保持UI的响应性,您将在后台运行冗长的任务。对于这样的东西使用NSOperation通常更好也更简单,所以请看这个类。
#3
2
Try it, there is no better answer. It’s not hard:
试试吧,没有更好的答案。这并不困难:
- (void) startInParallel {
for (int i=0; i<5; i++)
[self performSelectorInBackground:@selector(doSomeWork)];
}
Plus you might want to add some time measuring. Then, if you find out that running all the operations in parallel really makes sense, you can use NSOperations or GCD to do it. (I just hope that performSelectorInBackground does not have enough time cost to skew the test results.)
另外,你可能需要增加一些时间测量。然后,如果您发现并行运行所有的操作是有意义的,那么您可以使用NSOperations或GCD来完成它。(我只是希望performSelectorInBackground没有足够的时间来扭曲测试结果。)
#1
2
Use Grand Central Dispatch (aka GCD and libdispatch) and it'll just Do The Right Thing(tm).
使用Grand Central Dispatch(即GCD和libdispatch),它会做正确的事情(tm)。
If you call dispatch_async on one of the global queues, GCD will figure depending on system workload whether to spawn new threads to handle the operations you submit, or whether to run them in series on one thread.
如果您在一个全局队列上调用dispatch_async, GCD将根据系统工作负载确定是生成新的线程来处理您提交的操作,还是在一个线程上以串行方式运行它们。
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
// Do your calculations here
// If you want to run code on the main loop with the result of the calculations,
// do something like this
dispatch_async(dispatch_get_main_queue(), ^{
// Code to run on the main thread here
});
});
You could also use dispatch groups to run some code after all five calls have completed.
您还可以使用分派组在所有五个调用完成之后运行一些代码。
Take a look at the Concurrency Programming Guide and the GCD reference manual for details.
详细信息请参阅并发编程指南和GCD参考手册。
Update
更新
Also look at this article from Ars on GCD. They explain some of the details of how GCD decides how to schedule blocks.
还可以看看这篇来自GCD上Ars的文章。他们解释了GCD如何决定如何安排block的一些细节。
#2
2
If it's something like a network request, it will most probably be faster because you're spending time waiting without really using the processor (although spawning threads is not really the best way to deal with that). But if the method is doing some heavy calculations you won't gain anything. Still, to keep the UI responsive you would run lengthy tasks in the background. It's usually better and simpler to use NSOperation
for such things, so have a look at that class.
如果它是类似于网络请求的东西,那么它很可能会更快,因为您在没有真正使用处理器的情况下花费时间等待(尽管生成线程并不是真正最好的处理方法)。但是如果这个方法做了一些繁重的计算,你不会得到任何东西。不过,为了保持UI的响应性,您将在后台运行冗长的任务。对于这样的东西使用NSOperation通常更好也更简单,所以请看这个类。
#3
2
Try it, there is no better answer. It’s not hard:
试试吧,没有更好的答案。这并不困难:
- (void) startInParallel {
for (int i=0; i<5; i++)
[self performSelectorInBackground:@selector(doSomeWork)];
}
Plus you might want to add some time measuring. Then, if you find out that running all the operations in parallel really makes sense, you can use NSOperations or GCD to do it. (I just hope that performSelectorInBackground does not have enough time cost to skew the test results.)
另外,你可能需要增加一些时间测量。然后,如果您发现并行运行所有的操作是有意义的,那么您可以使用NSOperations或GCD来完成它。(我只是希望performSelectorInBackground没有足够的时间来扭曲测试结果。)