iOS,時(shí)間,NSDate,CFAbsoluteTimeGetCurrent,CACurrentMediaTime,mach_absolute_time


框架層:

NSDate 屬于Foundation

CFAbsoluteTimeGetCurrent() 屬于 CoreFoundatio

CACurrentMediaTime() 屬于 QuartzCore

mach_absolute_time屬于mach/mach_time

本質(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ì)被重置。

NSDate 和 dateWithTimeIntervalSince1970 是 NSDate的時(shí)間戳是指格林威治時(shí)間1970年01月01日00時(shí)00分00秒(北京時(shí)間1970年01月01日08時(shí)00分00秒)起至現(xiàn)在的總秒數(shù),是以秒為單位。常用于時(shí)間獲取和計(jì)算,精確度低,有的時(shí)候可能會(huì)出現(xiàn)偏差的時(shí)候,比如說(shuō)可能慢了兩秒啊,快了兩秒啊。

CFAbsoluteTimeGetCurrent()和[NSDate timeIntervalSinceReferenceDate]? 都是以2001/01/01 GMT為基準(zhǔn)時(shí)間,返回實(shí)例保存的時(shí)間與2001/01/01 GMT的時(shí)間間隔,是以秒為單位。常用于時(shí)間獲取和計(jì)算,精確度低,有的時(shí)候可能會(huì)出現(xiàn)偏差的時(shí)候,比如說(shuō)可能慢了兩秒啊,快了兩秒啊。

CACurrentMediaTime?返回的就是開(kāi)機(jī)后設(shè)備一共運(yùn)行了(設(shè)備休眠不統(tǒng)計(jì)在內(nèi))多少秒,是以秒為單位。只受設(shè)備重啟和休眠行為影響。重啟后重置。經(jīng)常被用于測(cè)試代碼的效率。

mach_absolute_time是一個(gè)CPU/總線依賴函數(shù),返回一個(gè)基于系統(tǒng)啟動(dòng)后的時(shí)鐘"嘀嗒"數(shù)。在macOS上可以確保它的行為,并且,它包含系統(tǒng)時(shí)鐘所包含的所有時(shí)間區(qū)域。單位是納秒,1納秒等于10的負(fù)9次方秒,精度高。是CACurrentMediaTime的基礎(chǔ)。經(jīng)常被用于經(jīng)常被用于測(cè)試代碼的效率。

個(gè)人推薦:

測(cè)試代碼的效率用mach_absolute_time理由是精度高。

時(shí)間點(diǎn)獲取用CFAbsoluteTimeGetCurrent理由是少計(jì)算30年的時(shí)間。

使用:


NSDate:

1.基本當(dāng)前時(shí)間

```

-(NSString*)getCurrentTimes{

? ? NSDateFormatter *formatter = [[NSDateFormatter alloc] init];

? ? // ----------設(shè)置你想要的格式,hh與HH的區(qū)別:分別表示12小時(shí)制,24小時(shí)制

? ? [formatter setDateFormat:@"YYYY-MM-dd HH:mm:ss"];

? ? //現(xiàn)在時(shí)間,你可以輸出來(lái)看下是什么格式

? ? NSDate *datenow = [NSDate date];

? ? //----------將nsdate按formatter格式轉(zhuǎn)成nsstring

? ? NSString *currentTimeString = [formatter stringFromDate:datenow];

? ? NSLog(@"currentTimeString =? %@",currentTimeString);

? ? return currentTimeString;

}

二、分別單獨(dú)獲得

————————————————

版權(quán)聲明:本文為CSDN博主「奔跑的Yancy」的原創(chuàng)文章,遵循 CC 4.0 BY-SA 版權(quán)協(xié)議,轉(zhuǎn)載請(qǐng)附上原文出處鏈接及本聲明。

原文鏈接:https://blog.csdn.net/lyxleft/article/details/79226774

```

2.

? NSTimeInterval? ChuoEndTime= [[NSDate date] timeIntervalSince1970];

? ? double time = [[NSString stringWithFormat:@"%f",ChuoEndTime] doubleValue];

? ?NSDate *myDate = [NSDate dateWithTimeIntervalSince1970:time];

NSDateFormatter *formatter = [NSDateFormatter new];

[formattersetDateFormat:@"YYYY-MM-dd HH:mm"];

? ? //將時(shí)間轉(zhuǎn)換為字符串

? ? NSString*timeS = [formatterstringFromDate:myDate];

? ? NSLog(@"時(shí)間戳轉(zhuǎn)時(shí)間%@",timeS);

獲取指定遞減時(shí)間的日,月,年數(shù)組:

@property (nonatomic,strong) NSArray *dataSource;

- (NSArray*)datesForFirstFewDaysWithFormat:(NSInteger)dateType{

? ? NSMutableArray *aMArray = [NSMutableArray array];

? ? for(NSIntegeri =_dataSource.count; i >0; i --) {

? ? ? ? //得到當(dāng)前的時(shí)間

? ? ? ? NSDate* mydate = [NSDatedate];

? ? ? ? NSDateFormatter* dateFormatter = [[NSDateFormatteralloc]init];

? ? ? ? NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];

? ? ? ? NSDateComponents*comps =nil;

? ? ? ? comps = [calendarcomponents:NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitMonth fromDate:mydate];

? ? ? ? NSDateComponents *adcomps = [[NSDateComponents alloc] init];

? ? ? ? if(dateType==0) {

? ? ? ? ? ? [dateFormattersetDateFormat:@"MM-dd"];

? ? ? ? ? ? [adcompssetYear:0];

? ? ? ? ? ? [adcompssetMonth:0];

? ? ? ? ? ? [adcompssetDay:-i];

? ? ? ? }elseif(dateType==1)

? ? ? ? {

? ? ? ? ? ? [dateFormattersetDateFormat:@"yyyy-MM"];

? ? ? ? ? ? [adcompssetYear:0];

? ? ? ? ? ? [adcompssetMonth:-i];

? ? ? ? ? ? [adcompssetDay:0];

? ? ? ? }elseif(dateType==2){

? ? ? ? ? ? [dateFormattersetDateFormat:@"yyyy"];

? ? ? ? ? ? [adcompssetYear:-i];

? ? ? ? ? ? [adcompssetMonth:0];

? ? ? ? ? ? [adcompssetDay:0];

? ? ? ? }

? ? ? ? NSDate*newdate = [calendardateByAddingComponents:adcompstoDate:mydateoptions:0];

? ? ? ? NSString*beforDate = [dateFormatterstringFromDate:newdate];

? ? ? ? ? ? ? ? [aMArrayaddObject:beforDate];

? ? }

? ? ? ? returnaMArray;

}


CFAbsoluteTimeGetCurrent:

?其中?CFAbsoluteTimeGetCurrent()?相當(dāng)于[[NSDate data] timeIntervalSinceReferenceDate];

? ? double date_s = CFAbsoluteTimeGetCurrent();

?NSDate *myDate = [NSDate dateWithTimeIntervalSinceReferenceDate:date_s];

? ? NSDateFormatter *formatter = [NSDateFormatter new];

? ? ? [formattersetDateFormat:@"YYYY-MM-dd HH:mm"];

? ? //將時(shí)間轉(zhuǎn)換為字符串

? ? NSString*timeS = [formatterstringFromDate:myDate];

? ? NSLog(@"時(shí)間戳轉(zhuǎn)時(shí)間%@",timeS);

CACurrentMediaTime:

for循環(huán)之間的就是測(cè)的代碼運(yùn)行了多少秒。

? double date_sN = CACurrentMediaTime();

? ? for(inti =0;i < test.count; i++) {

? ? ? ? sum +=1;

? ? }

? ? double date_e =? CACurrentMediaTime();

? ? NSLog(@"ForLoop Time: %f", date_e - date_sN);

mach_absolute_time:

for循環(huán)之間的就是測(cè)的代碼運(yùn)行了多少納秒。
@property(nonatomic,assign)UInt64 start;

@property(nonatomic,assign)UInt64 end;

?_start =mach_absolute_time();

? ? for(inti =0;i < test.count; i++) {

? ? ? ? sum +=1;

? ? }

? ? _end =mach_absolute_time();

? ? NSLog(@"--------%lli",_end-_start);

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 做App避免不了要和時(shí)間打交道,關(guān)于時(shí)間的處理,里面有不少門道,遠(yuǎn)不是一行API調(diào)用,獲取當(dāng)前系統(tǒng)時(shí)間這么簡(jiǎn)單。我...
    MrPeak閱讀 4,723評(píng)論 0 42
  • 前言 在iOS開(kāi)發(fā)過(guò)程中,經(jīng)常會(huì)和時(shí)間打交道。例如用戶在一個(gè)頁(yè)面停留的時(shí)間、兩個(gè)方法哪個(gè)執(zhí)行更快等等。之前對(duì)于這部...
    WQ_UESTC閱讀 3,576評(píng)論 0 12
  • 轉(zhuǎn)自:iOS關(guān)于時(shí)間的處理 做App避免不了要和時(shí)間打交道,關(guān)于時(shí)間的處理,里面有不少門道,遠(yuǎn)不是一行API調(diào)用,...
    咖啡綠茶1991閱讀 738評(píng)論 0 0
  • 背景 在我們編程的時(shí)候,可能經(jīng)常會(huì)有一些疑問(wèn): * 我們寫的某個(gè)方法的執(zhí)行效率是多少? * 方法 A 和 方法 B...
    咖啡綠茶1991閱讀 1,442評(píng)論 0 1
  • 時(shí)間 GMT:格林尼治時(shí)間理論上講,格林尼治時(shí)間的正午是太陽(yáng)橫穿格林尼治子午線的時(shí)間,作為世界各地區(qū)交流的的標(biāo)準(zhǔn)時(shí)...
    NapoleonY閱讀 381評(píng)論 0 0

友情鏈接更多精彩內(nèi)容