iPhone 开发 - NSTimer 时间精度而不是重新计算?

发布于 2024-08-06 14:56:03 字数 852 浏览 1 评论 0原文

我有一个小项目,它是一个倒计时直到午夜的计时器,我想知道我是否应该保持原样,每秒计算直到午夜的时间(在每 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

金兰素衣 2024-08-13 14:56:03

“没有理由不优化”这一说法是危险的。您需要一个理由来优化,而不是缺乏不优化的理由!该代码是一个相当合理的小片段,不会花费太多时间来执行。

至于准确性,保持准确时间的唯一合理方法是让系统为您做这件事,获取“现在”时间就是这样做的一种方法。将其掌握在自己手中可能会导致时钟出现错误,也许足以引起注意,也许不会。

至于优化,对其进行分析,看看它有多快,如果很糟糕,请修复它,如果没有,请继续做一些更有趣的事情!

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!

§普罗旺斯的薰衣草 2024-08-13 14:56:03

过早的优化总是不好,不要今天编写一年后无法调试的代码。切勿进行任何优化,除非您:

  1. 拥有一个可以运行的应用程序。
  2. 工作应用程序的性能实际上很差。
  3. 您已经通过检测来找到真正的漏洞。
  4. 您确信您使用的是最好的算法。
  5. 弄清楚如何在不牺牲代码清晰度的情况下进行优化。

综上所述,每秒检查一次当前时间永远不会降低您的性能。显然,您甚至还没有通过应用优化的要求 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:

  1. Have a working application.
  2. The working application have actual bad performance.
  3. You have done instrumentation to find the actual bootle-neck.
  4. You are sure you are using the best algorithm.
  5. Figured out how to optimize without sacrificing code clarity.

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文