获得更准确的时间测量
目前,我正在使用以下方法记录两个事件之间的时间(以十进制分钟为单位):
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您还可以使用 CFAbsoluteTimeGetCurrent() 返回一个包含以秒和几分之一秒为单位的时间的双精度值:
然后您可以计算以秒和几分之一秒为单位的经过时间:
我喜欢调试代码(#ifdef DEBUG)在我的应用程序委托中和我的第一个视图控制器中的 viewDidAppear 中报告已用的启动时间,以便我可以在开发时密切关注。
You can also use CFAbsoluteTimeGetCurrent() which returns a double containing time in seconds and fractions of a second:
Then later you can compute an elapsed time in seconds and fractions of a second:
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.
为什么不直接使用 NSDate 呢?
这将使您的测量精度达到亚秒级。
Why not just use NSDate?
That should give your measurement sub-second accuracy.
如果你想获得 Unix-y,你可以使用
gettimeofday
(参考):请务必将
#import
添加到您的文件中If you want to get Unix-y, you can use
gettimeofday
(reference):Be sure to add
#import <sys/time.h>
to your file或者,如果你真的想放下金属,你可以使用
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.