简体   繁体   English

iPhone背景线程与计时器性能

[英]iPhone Background Threads vs. Timers Performance

One of our requirment can be implemented either by running a NSTimer every 30 mins or a background thread/job, which polls specific application 'state' every one minute. 我们的要求之一可以通过每30分钟运行NSTimer或后台线程/作业(每隔一分钟轮询特定应用程序“状态”)来实现。

But I am wondering how CPU intensive this background job compared to NSTimer, if NOT much of a difference then we will go for background threads/jobs. 但是我想知道与NSTimer相比,此后台作业的CPU占用率如何,如果相差不大,那么我们将使用后台线程/作业。

Thanks 谢谢

Threads are very lightweight. 线程非常轻巧。

In fact you could very well do the NSTimer polling on main thread and then use GCD to perform the actual work on a private serial queue (on another thread) using an inline block. 实际上,您可以很好地在主线程上执行NSTimer轮询,然后使用内联块使用GCD在专用串行队列(在另一个线程上)上执行实际工作。 That way your code remains very readable while you do not sacrifice any performance. 这样一来,您的代码将保持很高的可读性,同时又不牺牲任何性能。

Note that NSTimer does not guarantee exact timing precision, but I assume that is not important. 请注意,NSTimer不能保证精确的计时精度,但是我认为这并不重要。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM