NSDate
屬于Foundation (單位秒县习,保留到微秒)
CFAbsoluteTimeGetCurrent()
屬于 CoreFoundatio(單位秒,保留到微秒配深,默認(rèn)為the reference date (epoch) is 00:00:00 1 January 2001)
相當(dāng)于[[NSDate data] timeIntervalSinceReferenceDate]
kCFAbsoluteTimeIntervalSince1970携添;(1970到現(xiàn)在的時(shí)間2000的時(shí)間差,單位秒)
與NSTimeIntervalSince1970 等同于 978307200.0
1970到當(dāng)前時(shí)間戳為(CFAbsoluteTimeGetCurrent() + kCFAbsoluteTimeIntervalSince1970)
CACurrentMediaTime()
屬于 QuartzCore(單位秒篓叶,保留到納秒)
偏移量的角度烈掠,
mach_absolute_time()(單位是納秒)
mach_absolute_time()轉(zhuǎn)化為CACurrentMediaTime()
CGFloat ComputeTimeBlock (void (^block)(void)) {
mach_timebase_info_data_t info;
if (mach_timebase_info(&info) != KERN_SUCCESS) return -1.0;
uint64_t start = mach_absolute_time ();
uint64_t nanos = start * info.numer / info.denom;
CGFloat test = (CGFloat)nanos / NSEC_PER_SEC;
return test;
}
本質(zhì)區(qū)別:
NSDate
或 CFAbsoluteTimeGetCurrent() 返回的時(shí)鐘時(shí)間將會(huì)會(huì)網(wǎng)絡(luò)時(shí)間同步羞秤,從時(shí)鐘
偏移量的角度,mach_absolute_time()(單位是納秒)
和 CACurrentMediaTime()
是基于內(nèi)建時(shí)鐘的左敌,能夠更精確更原子化地測(cè)量瘾蛋,并且不會(huì)因?yàn)橥獠繒r(shí)間變化而變化(例如時(shí)區(qū)變化、夏時(shí)制矫限、秒突變等),但它和系統(tǒng)的uptime有關(guān),系統(tǒng)重啟后CACurrentMediaTime()會(huì)被重置哺哼。
常見(jiàn)用法:
NSDate、CFAbsoluteTimeGetCurrent
()常用于日常時(shí)間叼风、時(shí)間戳的表示取董,與服務(wù)器之間的數(shù)據(jù)交互
CACurrentMediaTime() 常用于測(cè)試代碼的效率