NSDateFormatter 性能测试

NSDateFormatter 性能不完全测试

前几日KZ同学提醒,说看到有处我加的代码略微耗时(ps:kpi查的真严),看了看就是这货

    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"dd-MM-yyyy"];
    NSString *current = [dateFormatter stringFromDate:[NSDate date]];

其实就是NSDateFormatter初始化非常耗时,代码里也早已有写好的单例(ios7之后是线程安全,不需要每个线程都创建单例)供调用。
关于NSDateFormatter创建耗时的资料也是一搜一大把。

闲来无事便测试了一下,究竟有多耗时。

{
        begin = CACurrentMediaTime();
        for (int i = 0; i < count; i++) {
            formatter  = [[NSDateFormatter alloc] init];
            [formatter setDateFormat:@"yyyy-MM-dd"];
            string = [formatter stringFromDate:[NSDate date]];
        }
        end = CACurrentMediaTime();
        printf("NSDateFormatter:               %8.2f ms\n", (end - begin) * 1000);
    }
    {
        begin = CACurrentMediaTime();
        formatter  = [[NSDateFormatter alloc] init];
        for (int i = 0; i < count; i++) {
            [formatter setDateFormat:@"yyyy-MM-dd"];
            string = [formatter stringFromDate:[NSDate date]];
        }
        end = CACurrentMediaTime();
        printf("NSDateFormatter once:         %8.2f ms\n", (end - begin) * 1000);
    }

----测试1000次花费时间对比(8p/iOS12)-------
NSDateFormatter:                  86.05 ms
NSDateFormatter once:             4.01 ms

NSDateFormatter:                  84.80 ms
NSDateFormatter once:             4.36 ms

NSDateFormatter:                  80.20 ms
NSDateFormatter once:             3.89 ms

所以创建单例很有必要
上面可以看出两者之间消耗时间差距极大,而且还听过有人说应该 针对format格式创建对应的单例对象 ,那是NSDateFormatter初始化消耗太高吗??

for (int i = 0; i < count; i++) {
            begin = CACurrentMediaTime();
            formatter  = [[NSDateFormatter alloc] init];
            end = CACurrentMediaTime();
            a += (end-begin);
            
            begin = CACurrentMediaTime();
            [formatter setDateFormat:@"yyyy-MM-dd"];
            end = CACurrentMediaTime();
            b += (end-begin);
            
            begin = CACurrentMediaTime();
            string = [formatter stringFromDate:[NSDate date]];
            end = CACurrentMediaTime();
            c += (end-begin);
        }
        
        printf("NSDateFormatter:alloc               %8.2f ms\n", a * 1000);
        printf("NSDateFormatter:setFormat           %8.2f ms\n", b * 1000);
        printf("NSDateFormatter:stringFromDate      %8.2f ms\n", c * 1000);
----各个方法分开测试1000次花费时间对比(8p/iOS12)-------
NSDateFormatter:alloc                   9.44 ms
NSDateFormatter:setFormat               0.53 ms
NSDateFormatter:stringFromDate         65.40 ms

NSDateFormatter:alloc                  10.55 ms
NSDateFormatter:setFormat               0.58 ms
NSDateFormatter:stringFromDate         73.52 ms

NSDateFormatter:alloc                  10.11 ms
NSDateFormatter:setFormat               0.56 ms
NSDateFormatter:stringFromDate         70.50 ms

从上面可以看出,实际最耗时的方法为初次调用stringFromDate/dateFromString。

/*
生成不同format格式的对象对比
*/
{
        begin = CACurrentMediaTime();
        NSDateFormatter * formatter1  = [[NSDateFormatter alloc] init];
        NSDateFormatter * formatter2  = [[NSDateFormatter alloc] init];
        NSDateFormatter * formatter3  = [[NSDateFormatter alloc] init];
        NSDateFormatter * formatter4  = [[NSDateFormatter alloc] init];
        for (int i = 0; i < count/4; i++) {
            [formatter1 setDateFormat:@"yyyy-MM-dd"];
            string = [formatter1 stringFromDate:[NSDate date]];
            [formatter2 setDateFormat:@"MM-dd-yyyy"];
            string = [formatter2 stringFromDate:[NSDate date]];
            [formatter3 setDateFormat:@"MM-dd"];
            string = [formatter3 stringFromDate:[NSDate date]];
            [formatter4 setDateFormat:@"MM-yyyy"];
            string = [formatter4 stringFromDate:[NSDate date]];
        }
        end = CACurrentMediaTime();
        printf("NSDateFormatter:different format  %8.2f ms\n", (end - begin) * 1000);
    }
    {
        begin = CACurrentMediaTime();
        formatter  = [[NSDateFormatter alloc] init];
        for (int i = 0; i < count/4; i++) {
            [formatter setDateFormat:@"yyyy-MM-dd"];
            string = [formatter stringFromDate:[NSDate date]];
            [formatter setDateFormat:@"MM-dd-yyyy"];
            string = [formatter stringFromDate:[NSDate date]];
            [formatter setDateFormat:@"MM-dd"];
            string = [formatter stringFromDate:[NSDate date]];
            [formatter setDateFormat:@"MM-yyyy"];
            string = [formatter stringFromDate:[NSDate date]];
        }
        end = CACurrentMediaTime();
        printf("NSDateFormatter:                   %8.2f ms\n", (end - begin) * 1000);
    }
------不同format格式对象与同一对象设置不同格式对比------
NSDateFormatter:different format     27.89 ms
NSDateFormatter:                     16.16 ms
NSDateFormatter:different format     25.33 ms
NSDateFormatter:                     14.11 ms
NSDateFormatter:different format     28.51 ms
NSDateFormatter:                     15.62 ms

个人觉得创建一个单例即可,没有必要按格式创建多个,dateFormatter初次使用时消耗较大,设置format格式却并没有什么影响。

dateFromString方法我也测试过,结论基本一致便不一一唠叨了

谢谢阅读~~_

你可能感兴趣的:(NSDateFormatter 性能测试)