(转)Foundation-性能优化之NSDateFormatter

性能优化之NSDateFormatter

为何要优化NSDateFormatter?
html

首先,过分的建立NSDateFormatter用于NSDateNSString之间转换,会致使App卡顿,打开Profile工具查一下性能,你会发现这种操做占CPU比例是很是高的。据官方说法,建立NSDateFormatter代价是比较高的,若是你使用的很是频繁,那么建议你缓存起来,缓存NSDateFormatter必定能提升效率。缓存

Creating a date formatter is not a cheap operation. If you are likely to use a formatter frequently, it is typically more efficient to cache a single instance than to create and dispose of multiple instances. One approach is to use a static variable安全

优化方式有哪些?

a.延迟转换

即只有在UI须要使用转换结果时在进行转换。性能优化

b.Cache in Memory

根据NSDateFormatter线程安全性,不一样的iOS系统版本内存缓存以下:app

  • prior to iOS 7ide

若是直接采用静态变量进行存储,那么可能就会存在线程安全问题,在iOS 7以前,NSDateFormatter是非线程安全的,所以可能就会有两条或以上的线程同时访问同一个日期格式化对象,从而致使App崩溃。函数

+ (NSDateFormatter *)cachedDateFormatter {

NSMutableDictionary *threadDictionary = [[NSThread currentThread] threadDictionary];

NSDateFormatter *dateFormatter = [threadDictionary objectForKey:@"cachedDateFormatter"];

if (!dateFormatter) {

dateFormatter = [[NSDateFormatter alloc] init];

[dateFormatter setLocale:[NSLocale currentLocale]];

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

[threadDictionary setObject:dateFormatter forKey:@"cachedDateFormatter"];

}

return dateFormatter;

}
  • iOS 7 or later工具

在iOS 七、macOS 10.9及以上系统版本,NSDateFormatter都是线程安全的,所以咱们无需担忧日期格式化对象在使用过程当中被另一条线程给修改,为了提升性能,咱们还能够在上述代码块中进行简化(除去冗余部分)。性能

static NSDateFormatter *cachedDateFormatter = nil;

+ (NSDateFormatter *)cachedDateFormatter {

// If the date formatters aren't already set up, create them and cache them for reuse.

if (!dateFormatter) {

dateFormatter = [[NSDateFormatter alloc] init];

[dateFormatter setLocale:[NSLocale currentLocale]];

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

}

return dateFormatter;

}

若是缓存了日期格式化或者是其余依赖于current locale的对象,那么咱们应该监听NSCurrentLocaleDidChangeNotification通知,当current locale变化时及时更新被缓存的日期格式化对象。优化

In theory you could use an auto-updating locale (autoupdatingCurrentLocale) to create a locale that automatically accounts for changes in the user’s locale settings. In practice this currently does not work with date formatters.

Apple Threading Programming Guide

c.利用标准C语言库

若是时间日期格式是固定的,咱们能够采用C语言中的strptime函数,这样更加简单高效。

- (NSDate *) easyDateFormatter{

time_t t;

struct tm tm;

char *iso8601 = "2016-09-18";

strptime(iso8601, "%Y-%m-%d", &tm);

tm.tm_isdst = -1;

tm.tm_hour = 0;//当tm结构体中的tm.tm_hour为负数,会致使mktime(&tm)计算错误

/**

//NSString *iso8601String = @"2016-09-18T17:30:08+08:00";

//%Y-%m-%d [iso8601String cStringUsingEncoding:NSUTF8StringEncoding]

{

tm_sec = 0

tm_min = 0

tm_hour = 0

tm_mday = 18

tm_mon = 9

tm_year = 116

tm_wday = 2

tm_yday = 291

tm_isdst = 0

tm_gmtoff = 28800

tm_zone = 0x00007fd9b600c31c "CST"

}

ISO8601时间格式:2004-05-03T17:30:08+08:00 参考Wikipedia

*/

t = mktime(&tm);

//http://pubs.opengroup.org/onlinepubs/9699919799/functions/mktime.html

//secondsFromGMT: The current difference in seconds between the receiver and Greenwich Mean Time.

return [NSDate dateWithTimeIntervalSince1970:t + [[NSTimeZone localTimeZone] secondsFromGMT]];

}

相关资料:

Date Formate Patterns :

1639372-1accc35e3ae104bd.png

Standard C library

ISO_8601

相关文章
相关标签/搜索