時間損耗
time(&temp)
返回從CUT(Coordinated Universal Time)時間1970年1月1日00:00:00(稱為UNIX系統(tǒng)的Epoch時間)到當(dāng)前時刻的秒數(shù)
clock()
返回從“開啟這個程序進程”到“程序中調(diào)用clock()
函數(shù)”時之間的CPU時鐘計時單元(clock tick)數(shù); 比如姻僧,sleep(5)并不占用cpu資源蒲牧,導(dǎo)致即使sleep
了5秒,最后通過clock()
來計算時間損耗冰抢,仍然不會將這5秒算入損耗之中。
CFAbsoluteTimeGetCurrent()
返回的時鐘時間將與會網(wǎng)絡(luò)時間同步翠订,
mach_absolute_time()
和 CACurrentMediaTime()
是基于內(nèi)建時鐘的遵倦,能夠更精確更原子化地測量,并且不會因為外部時間變化而變化(例如時區(qū)變化梧躺、夏時制、秒突變等),但它和系統(tǒng)的uptime有關(guān),系統(tǒng)重啟后CACurrentMediaTime()會被重置棘脐。(比如電腦開機龙致,手機開機到調(diào)用函數(shù))
CFTimeInterval GetCFAbsoluteTimeWithBlock(dispatch_block_t block) {
CFAbsoluteTime currentCFTime = CFAbsoluteTimeGetCurrent();
block();
CFAbsoluteTime lastCFTime = CFAbsoluteTimeGetCurrent();
return lastCFTime - currentCFTime;
}
CFTimeInterval GetCACurrentMediaTimeWithBlock(dispatch_block_t block) {
CFTimeInterval currentCATime = CACurrentMediaTime();
block();
CFTimeInterval lastCATime = CACurrentMediaTime();
return lastCATime - currentCATime;
}
CFTimeInterval GetUint64WithBlock(dispatch_block_t block) {
uint64_t currentTime = mach_absolute_time();
block();
uint64_t lastTime = mach_absolute_time();
return (1e-9*(lastTime - currentTime));
}
CFTimeInterval GetClockWithBlock(dispatch_block_t block) {
clock_t currentTime = clock();
block();
clock_t lastTime = clock();
return (CFTimeInterval)(lastTime - currentTime)/CLOCKS_PER_SEC;
}
NSDate 目代、CFAbsoluteTimeGetCurrent嗤练、CACurrentMediaTime 的區(qū)別
iOS 性能優(yōu)化之業(yè)務(wù)性能監(jiān)控
字符串查找
__block NSString *testString = @"honzon now 25 years old, now live in beijing. honzon like watching moves, reading books and doing many other things.";
__block NSString *like = @"honzon";
NSLog(@"NSScanner used time: %f", GetCFAbsoluteTimeWithBlock(^{
NSScanner *scanner = [NSScanner scannerWithString:testString];
scanner.scanLocation = 0;
while (![scanner isAtEnd]) {
if ([scanner scanUpToString:like intoString:NULL]) {
NSLog(@"NSScanner success");
break;
}
scanner.scanLocation++;
}
}));
NSLog(@"rangeOfString used time: %f", GetCFAbsoluteTimeWithBlock(^{
if ([testString rangeOfString:like].length > 0) {
NSLog(@"rangeOfString success");
}
}));
NSLog(@"containsString used time: %f", GetCFAbsoluteTimeWithBlock(^{
//在iOS8以后在讶,還可以用下面的方法來判斷是否包含某字符串:
if ([testString containsString:like]) {
NSLog(@"containsString success");
}
}));
NSLog(@"NSPredicate used time: %f", GetCFAbsoluteTimeWithBlock(^{
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF CONTAINS %@",like];
if([predicate evaluateWithObject:testString]) {
NSLog(@"NSPredicate success");
}
}));
NSLog(@"NSTextCheckingResult used time: %f", GetCFAbsoluteTimeWithBlock(^{
NSError *error = NULL;
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:like options:NSRegularExpressionCaseInsensitive error:&error];
NSTextCheckingResult *result = [regex firstMatchInString:testString options:0 range:NSMakeRange(0, [testString length])];
if (result) {
NSLog(@"NSTextCheckingResult success");
}
}));
NSLog(@"hasPrefix used time: %f", GetCFAbsoluteTimeWithBlock(^{
if ([testString hasPrefix:like]) {//hasSuffix
NSLog(@"hasPrefix success");
}
}));
//查找文字(honzon)位于最開始位置 containsString(0.000035) > hasPrefix(0.000042) > rangeOfString(0.000046) > NSPredicate(0.000385) > NSScanner(0.000808) > NSRegularExpression(0.003134)
//查找文字(like)位于最中間位置 containsString(0.000038) > rangeOfString(0.000061) > NSScanner(0.000887) > NSPredicate(0.000363) > NSRegularExpression(0.002953)
//查找文字(things.)位于最末端位置 containsString(0.000088) > hasSuffix(0.000089) > rangeOfString(0.000113) > NSPredicate(0.000416) > NSScanner(0.000841) > NSRegularExpression(0.003864)
在iOS8之前构哺,可以盡量使用rangeOfString:
方法來查找字符串,之后的話残拐,使用containsString
會更加快速碟嘴。