获得更准确的时间测量

发布于 2024-12-01 14:30:21 字数 1122 浏览 1 评论 0原文

目前,我正在使用以下方法记录两个事件之间的时间(以十进制分钟为单位):

NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSCalendarUnit unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit | NSWeekCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit;
NSDate *date = [NSDate date];
NSDateComponents *dateComponents =  [calendar components:unitFlags fromDate:date];

CGFloat hour = [dateComponents hour];
CGFloat minute = [dateComponents minute];
CGFloat second = [dateComponents second];

//set start time in decimal minutes
CGFloat startTime = ((hour*3600.0)+(minute*60.0)+(second))*.016666;

//do work...

//set end time in decimal minutes
CGFloat endTime = ((hour*3600.0)+(minute*60.0)+(second))*.016666;

//calculate time elapsed in decimal minutes
CGFloat totalTime = endTime-startTime;

我遇到的问题是,我实际上只能获取以秒为单位的时间,然后转换为十进制分钟。因此,我得到的唯一值是 0.016663 (十进制分钟中的一秒)、0.033325 (十进制分钟中的两秒)、0.049988(十进制分钟中的三秒)等。我没有得到中间的任何值。有什么办法可以得到更准确的时间吗?我检查了 NSDateComponents 是否有毫秒属性或其他属性,但我找不到任何属性。

谢谢!

Currently I'm recording the time between two events in decimal minutes using :

NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSCalendarUnit unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit | NSWeekCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit;
NSDate *date = [NSDate date];
NSDateComponents *dateComponents =  [calendar components:unitFlags fromDate:date];

CGFloat hour = [dateComponents hour];
CGFloat minute = [dateComponents minute];
CGFloat second = [dateComponents second];

//set start time in decimal minutes
CGFloat startTime = ((hour*3600.0)+(minute*60.0)+(second))*.016666;

//do work...

//set end time in decimal minutes
CGFloat endTime = ((hour*3600.0)+(minute*60.0)+(second))*.016666;

//calculate time elapsed in decimal minutes
CGFloat totalTime = endTime-startTime;

The issue that I'm having is that I am really only getting the time in seconds and then converting to decimal minutes. So, the only values that I'm getting are 0.016663 (one second in decimal minutes), 0.033325 (two seconds in decimal minutes), 0.049988 (three seconds in decimal minutes), etc. I'm not getting any values in between. Is there any way I can get a more accurate time? I checked to see if NSDateComponents has a millisecond property or whatnot, but I couldnt find any.

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

与风相奔跑 2024-12-08 14:30:21

您还可以使用 CFAbsoluteTimeGetCurrent() 返回一个包含以秒和几分之一秒为单位的时间的双精度值:

CFAbsoluteTime startTime = CFAbsoluteTimeGetCurrent();

然后您可以计算以秒和几分之一秒为单位的经过时间:

elapsed = CFAbsoluteTimeGetCurrent() - startTime;

NSLog(@"Elapsed Time: %0.3f seconds", elapsed);

我喜欢调试代码(#ifdef DEBUG)在我的应用程序委托中和我的第一个视图控制器中的 viewDidAppear 中报告已用的启动时间,以便我可以在开发时密切关注。

You can also use CFAbsoluteTimeGetCurrent() which returns a double containing time in seconds and fractions of a second:

CFAbsoluteTime startTime = CFAbsoluteTimeGetCurrent();

Then later you can compute an elapsed time in seconds and fractions of a second:

elapsed = CFAbsoluteTimeGetCurrent() - startTime;

NSLog(@"Elapsed Time: %0.3f seconds", elapsed);

I like to have debug code (#ifdef DEBUG) in my app delegate and in viewDidAppear in my first view controller to report the elapsed startup time so I can keep an eye on that while developing.

随心而道 2024-12-08 14:30:21

为什么不直接使用 NSDate 呢?

NSDate *date1, *date2;
NSTimeInterval timeInSeconds;

date1 = [NSDate date];

// Do work

date2 = [NSDate date];
timeInSeconds = [date2 timeIntervalSinceDate:date1];

// Or, without using date2:

timeInSeconds = -[date1 timeIntervalSinceNow];

这将使您的测量精度达到亚秒级。

Why not just use NSDate?

NSDate *date1, *date2;
NSTimeInterval timeInSeconds;

date1 = [NSDate date];

// Do work

date2 = [NSDate date];
timeInSeconds = [date2 timeIntervalSinceDate:date1];

// Or, without using date2:

timeInSeconds = -[date1 timeIntervalSinceNow];

That should give your measurement sub-second accuracy.

開玄 2024-12-08 14:30:21

如果你想获得 Unix-y,你可以使用 gettimeofday (参考):

struct timeval tv;
gettimeofday(&tv, NULL);
NSLog(@"seconds: %d, microseconds: %d\n", tv.tv_sec, tv.tv_usec);

请务必将 #import 添加到您的文件中

If you want to get Unix-y, you can use gettimeofday (reference):

struct timeval tv;
gettimeofday(&tv, NULL);
NSLog(@"seconds: %d, microseconds: %d\n", tv.tv_sec, tv.tv_usec);

Be sure to add #import <sys/time.h> to your file

凉墨 2024-12-08 14:30:21

或者,如果你真的想放下金属,你可以使用mach_absolute_time(),按照这个SO Q/A 和/或 Apple 文档

Or if you really want to get down the metal, you can use mach_absolute_time(), as per this SO Q/A and/or Apple docs.

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