NSDateFormatter 性能不完全測試
前幾日KZ同學(xué)提醒话速,說看到有處我加的代碼略微耗時(ps:kpi查的真嚴(yán))癞尚,看了看就是這貨
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"dd-MM-yyyy"];
NSString *current = [dateFormatter stringFromDate:[NSDate date]];
其實就是NSDateFormatter初始化非常耗時氏身,代碼里也早已有寫好的單例(ios7之后是線程安全床估,不需要每個線程都創(chuàng)建單例)供調(diào)用雇寇。
關(guān)于NSDateFormatter創(chuàng)建耗時的資料也是一搜一大把赵颅。
閑來無事便測試了一下虽另,究竟有多耗時。
{
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
所以創(chuàng)建單例很有必要
上面可以看出兩者之間消耗時間差距極大饺谬,而且還聽過有人說應(yīng)該 針對format格式創(chuàng)建對應(yīng)的單例對象 捂刺,那是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
從上面可以看出族展,實際最耗時的方法為初次調(diào)用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格式對象與同一對象設(shè)置不同格式對比------
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
個人覺得創(chuàng)建一個單例即可拔鹰,沒有必要按格式創(chuàng)建多個仪缸,dateFormatter初次使用時消耗較大,設(shè)置format格式卻并沒有什么影響列肢。
dateFromString方法我也測試過恰画,結(jié)論基本一致便不一一嘮叨了
謝謝閱讀~~_