性能优化之NSDateFormatter

简介:

为什么要优化NSDateFormatter?

首先,过度的创建NSDateFormatter用于NSDate与NSString之间转换,会导致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系统版本内存缓存如下:

  • prior to iOS 7

如果直接采用静态变量进行存储,那么可能就会存在线程安全问题,在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 7、macOS 10.9及以上系统版本,NSDateFormatter都是线程安全的,因此我们无需担心日期格式化对象在使用过程中被另外一条线程给修改,为了提高性能,我们还可以在上述代码块中进行简化(除去冗余部分)。

static NSDateFormatter *cachedDateFormatter = nil;

+ (NSDateFormatter *)cachedDateFormatter {NSMutableDictionary// 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函数,这样更加简单高效。

 

}

本文转自lzwxx 51CTO博客,原文链接:
http://blog.51cto.com/13064681/1943390


相关文章
|
人工智能 IDE 开发工具
寻找Cursor的替代品:10款AI编程工具深度评测与推荐·优雅草卓伊凡
寻找Cursor的替代品:10款AI编程工具深度评测与推荐·优雅草卓伊凡
10975 18
寻找Cursor的替代品:10款AI编程工具深度评测与推荐·优雅草卓伊凡
|
开发者 索引 容器
Flutter开发笔记:Flutter 布局相关组件
Flutter开发笔记:Flutter 布局相关组件
1127 0
|
存储
ARM64特有一些的汇编指令
ARM64特有的汇编指令
3445 0
ARM64特有一些的汇编指令
|
存储 语音技术
HarmonyOS学习路之开发篇—设备管理(设置项)
应用程序可以对系统各类设置项进行查询。例如,三方应用提前注册飞行模式设置项的回调,当用户通过系统设置修改终端的飞行模式状态时,三方应用会检测到此设置项发生变化并进行适配。如检测到飞行模式开启,将进入离线状态;检测到飞行模式关闭,其将重新获取在线数据。
|
缓存 算法 API
HarmonyOS学习路之开发篇—设备管理(位置开发)
移动终端设备已经深入人们日常生活的方方面面,如查看所在城市的天气、新闻轶事、出行打车、旅行导航、运动记录。这些习以为常的活动,都离不开定位用户终端设备的位置。
|
索引 容器
NSIndexSet 简介
NSIndexSet (以及它的可修改子类, NSMutableIndexSet) 是一个排好序的,无重复元素的整数集合。它看上去有点像 支持离散整数的 NSRange .它能用于快速查找特定范围的值的索引,也能用于快速计算交集, 同时,Foundation collection class 提供了很多好用的方法,方便你使用 NSIndexSet.
706 0
NSIndexSet 简介
|
SQL 安全 数据库
|
5天前
|
JavaScript API iOS开发
DeepSeek Harness下载、安装、配置一篇搞定(2026整理,送常用插件)
DeepSeek Harness(DSH)是DeepSeek于2026年开源的MIT协议AI Agent框架,支持插件化组装模型、工具、沙箱与UI,兼容主流大模型。本文提供免Node.js的桌面端安装包(Win/macOS),含详细图文教程及热门插件推荐。
2440 17

热门文章

最新文章