dispatch_time和dispatch_walltime有什么区别?在哪种情况下最好使用其中一种? [英] What is the difference between dispatch_time and dispatch_walltime and in what situations is better to use one or the other?
问题描述
我知道dispatch_time
是根据设备时钟的时间,如果设备进入睡眠状态,时钟也将进入睡眠状态.另一方面,dipatch_walltime
是根据挂钟的时间,它永远不会进入睡眠状态.我的问题是,在不同的情况下使用一种或另一种在性能方面是否有区别?有人可以给我更多细节,因为Apple文档并不详尽.
I know that dispatch_time
is the time according to device clock and if the device goes to sleep the clock sleeps too. On the other hand the dipatch_walltime
is time according to wall clock, which never goes to sleep. My questions is, is there any difference, performance-wise or other, to use one or the other in different situations? Can somebody give me some more details, because the Apple docs are not exhaustive.
例如,我正在编写一个以特定间隔运行的Timer
类.还有余地可能是10到30秒.从性能角度考虑,我应该使用哪个dispatch_time
或dispatch_walltime
.
For example I'm writing a Timer
class which operates on certain intervals. Also the leeway could be 10 to 30 secs. Which one should I use dispatch_time
or dispatch_walltime
, performance-wise.
推荐答案
dispatch_time
停止运行.
dispatch_walltime
继续运行,因为现实世界继续运行.
dispatch_time
stops running when your computer goes to sleep.
dispatch_walltime
continues running because the real world continues running.
因此,假设您要从现在开始1个小时执行一次操作,但是5分钟后您的计算机将进入休眠状态50分钟.然后:
So suppose you want to do an action 1 hour from now, but after 5 minutes your computer goes to sleep for 50 minutes. Then:
-
dispatch_walltime
将在计算机唤醒后的5分钟后的一个小时内执行.
dispatch_walltime
will execute an hour from now, 5 minutes after the computer wakes up.
dispatch_time
将在计算机运行一个小时后执行,即唤醒后的55分钟(从现在起1小时50分钟).
dispatch_time
will execute after the computer is running for an hour, that is 55 minutes after it wakes up, 1 hour and 50 minutes from now.
这篇关于dispatch_time和dispatch_walltime有什么区别?在哪种情况下最好使用其中一种?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!