时间损耗 & 字符串查找

时间损耗

time(&temp)返回从CUT(Coordinated Universal Time)时间1970年1月1日00:00:00(称为UNIX系统的Epoch时间)到当前时刻的秒数

clock()返回从“开启这个程序进程”到“程序中调用clock()函数”时之间的CPU时钟计时单元(clock tick)数; 比如,sleep(5)并不占用cpu资源,导致即使sleep了5秒,最后通过clock()来计算时间损耗,仍然不会将这5秒算入损耗之中。

CFAbsoluteTimeGetCurrent() 返回的时钟时间将与会网络时间同步,

mach_absolute_time()CACurrentMediaTime()是基于内建时钟的,能够更精确更原子化地测量,并且不会因为外部时间变化而变化(例如时区变化、夏时制、秒突变等),但它和系统的uptime有关,系统重启后CACurrentMediaTime()会被重置。(比如电脑开机,手机开机到调用函数)

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 的区别

iOS 性能优化之业务性能监控

字符串查找

__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会更加快速。

你可能感兴趣的:(时间损耗 & 字符串查找)