您好,欢迎来到二三四教育网。
搜索
您的当前位置:首页NSDate、CFAbsoluteTimeGetCurrent

NSDate、CFAbsoluteTimeGetCurrent

来源:二三四教育网

NSDate 属于 Foundation
CFAbsoluteTimeGetCurrent() 属于 CoreFoundation
CACurrentMediaTime() 属于 QuartzCore
Foundation是由CoreFoundation构建的,所以咱们可以大胆猜测CFAbsoluteTimeGetCurrent()和[[NSDate date] timeIntervalSinceReferenceDate]最终是由由同一个方法实现的。

我们追踪一下CFAbsoluteTimeGetCurrent()查看代码:

typedef double CFTimeInterval;
typedef CFTimeInterval CFAbsoluteTime;
/* absolute time is the time interval since the reference date */
/* the reference date (epoch) is 00:00:00 1 January 2001. */

CF_EXPORT
CFAbsoluteTime CFAbsoluteTimeGetCurrent(void);

从CFTimeInterval的定义和注释可以看出,CFAbsoluteTimeGetCurrent(void)返回的时间就是当前时间相对与reference date的时间。
CFTimeInterval 是对double 的重命名。
而NSTimeInterval 也是对double 的重命名。
它们之间的关系就可想而已了!
CFAbsoluteTimeGetCurrent() 其实等价于 [[NSDate new] timeIntervalSinceReferenceDate] 。

而CACurrentMediaTime()就不一样了, CACurrentMediaTime()是基于内建时钟的,能够更精确更原子化地测量,并且不会因为外部时间变化而变化(例如时区变化、夏时制、秒突变等),但它和系统的uptime有关,系统重启后CACurrentMediaTime()会被重置。

总结,CACurrentMediaTime()才是最适用于测试代码效率的。

Copyright © 2019- how234.cn 版权所有 赣ICP备2023008801号-2

违法及侵权请联系:TEL:199 1889 7713 E-MAIL:2724546146@qq.com

本站由北京市万商天勤律师事务所王兴未律师提供法律服务