iPhone 开发 - NSTimer 时间精度而不是重新计算?
我有一个小项目,它是一个倒计时直到午夜的计时器,我想知道我是否应该保持原样,每秒计算直到午夜的时间(在每 1 秒调用一次的 NSTimer 方法内):
NSDate *now = [[NSDate alloc] init];
NSDateComponents *dateComponents = [[self gregorian] components:
(NSHourCalendarUnit |
NSMinuteCalendarUnit |
NSSecondCalendarUnit) fromDate:now];
[now release];
NSUInteger hour = 23 - [dateComponents hour];
NSUInteger min = 59 - [dateComponents minute];
NSUInteger sec = 59 - [dateComponents second];
NSString *time = [[NSString alloc] initWithFormat:@"%02d:%02d:%02d",
hour, min, sec];
[[self lblCountDown] setText:
[time stringByReplacingOccurrencesOfString:@"1" withString:@" 1"]];
[time release];
或者应该我只是第一次计算,然后每隔一次就每次减去一秒,因为它同步到每次调用 1 秒?我不担心这会花费超过 1 秒的时间,但如果结果与其他方式相同,则没有理由不进行优化..所以无论如何,我应该这样做吗?为什么?
谢谢。
I have a little project that is a timer that counts down 'til midnight, and I was wondering If I should leave it as it is, calculating the time until midnight each second (inside NSTimer method that gets called every 1 second):
NSDate *now = [[NSDate alloc] init];
NSDateComponents *dateComponents = [[self gregorian] components:
(NSHourCalendarUnit |
NSMinuteCalendarUnit |
NSSecondCalendarUnit) fromDate:now];
[now release];
NSUInteger hour = 23 - [dateComponents hour];
NSUInteger min = 59 - [dateComponents minute];
NSUInteger sec = 59 - [dateComponents second];
NSString *time = [[NSString alloc] initWithFormat:@"%02d:%02d:%02d",
hour, min, sec];
[[self lblCountDown] setText:
[time stringByReplacingOccurrencesOfString:@"1" withString:@" 1"]];
[time release];
or should I just calculate that the first time, and then every other time just subtract one second each time since its synced to 1 second per call? I'm not worried that this will take longer than 1 second, but if the result will be the same the other way there's no reason not to optimize.. so anyway, should I do it that way? And why?
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
“没有理由不优化”这一说法是危险的。您需要一个理由来优化,而不是缺乏不优化的理由!该代码是一个相当合理的小片段,不会花费太多时间来执行。
至于准确性,保持准确时间的唯一合理方法是让系统为您做这件事,获取“现在”时间就是这样做的一种方法。将其掌握在自己手中可能会导致时钟出现错误,也许足以引起注意,也许不会。
至于优化,对其进行分析,看看它有多快,如果很糟糕,请修复它,如果没有,请继续做一些更有趣的事情!
The statement "there's no reason not to optimize" is a dangerous one. You need a reason to optimize, not a lack of a reason not to! That code is a fairly reasonable little snip that won't take much time to execute.
As for accuracy, the only reasonable way to keep accurate time is to let the system do it for you, getting the "now" time is a way of doing that. Taking it into your own hands will likely lead to the clock drifting into error, maybe enough to notice, maybe not.
As for the optimization, profile it, see how fast it is, if it sucks, fix it, if it doesn't, move on to something more fun!
过早的优化总是不好,不要今天编写一年后无法调试的代码。切勿进行任何优化,除非您:
综上所述,每秒检查一次当前时间永远不会降低您的性能。显然,您甚至还没有通过应用优化的要求 2。
Premature optimization is always BAD, do not write code today that you can not debug a year later. Never do any optimization until you:
With that all said and done, checking for the current time once every second can never degenerate your performance. So obviously you have not even passed requirement 2 for applying optimizations.