參考原文Swift【優(yōu)化Swift項(xiàng)目編譯時(shí)間】
借助第三方插件
BuildTimeAnalyzer-for-Xcode
CA689C72-F143-425B-BD12-A30F2A8B8881.png
使用方法
1.下載項(xiàng)目后,運(yùn)行會(huì)彈出一個(gè)窗口303095EE-9603-42B4-8615-594A850ED4B4.png
00BF99B2-0F1C-4D41-BEAA-D8F5317C6882.png
2.按照上面提示,打開(kāi)自己需要編譯的項(xiàng)目,在build setting中,設(shè)置Other Swift Flags為-Xfrontend -debug-time-function-bodies
然后clean ,編譯,然后就看到最上面第一個(gè)示例圖
通過(guò)參考其他博客,并且親測(cè)各種方法
減少編譯時(shí)長(zhǎng)的方法
1.使用自定義或者第三方組件,建議用framework的方式集成到項(xiàng)目中,會(huì)減少編譯時(shí)長(zhǎng)
在網(wǎng)上找了個(gè)第三方的下拉刷新,這個(gè)第三方?jīng)]有支持carthage
然后自己制作了一個(gè)framework進(jìn)行測(cè)試
直接用原代碼無(wú)framework
先clean 清理DerivedData緩存
1. 21s 2.14s 3. 13s 4. 13s 5. 13s 6. 13s 7. 12s
排除 第一個(gè)偏差大的
78/6 = 13
如果僅僅是clean然后編譯 6s 6s 5s 5s 5s 5s 總32 平均 =5.33333
用framework
先clean 清理DerivedData緩存
1. 16s 2. 12s 3. 12s 4.12s 5.10s 46/4 = 11.5
僅僅是clean 5s 3s 4s 6s 4s 4s 總26 平均 = 4.33333
2.明確指明類型痘昌,不要讓編譯器去自動(dòng)推導(dǎo)
這個(gè)結(jié)論其實(shí)不準(zhǔn)確,親測(cè)如下
// 類型聲明之后: 48.89ms 47.78ms clean之后: 50.38ms 49.00ms
// 自動(dòng)推導(dǎo) 45.62 47.56ms 49.19ms clean之后: 50.21ms 49.23ms
// : Dictionary<String, Any>
let myCompany = [
"employees": [
"employee 1": ["attribute": "value"],
"employee 2": ["attribute": "value"],
"employee 3": ["attribute": "value"],
"employee 4": ["attribute": "value"],
"employee 5": ["attribute": "value"],
"employee 6": ["attribute": "value"],
"employee 7": ["attribute": "value"],
"employee 8": ["attribute": "value"],
"employee 9": ["attribute": "value"],
"employee 10": ["attribute": "value"],
"employee 11": ["attribute": "value"],
"employee 12": ["attribute": "value"],
"employee 13": ["attribute": "value"],
"employee 14": ["attribute": "value"],
"employee 15": ["attribute": "value"],
"employee 16": ["attribute": "value"],
"employee 17": ["attribute": "value"],
"employee 18": ["attribute": "value"],
"employee 19": ["attribute": "value"],
"employee 20": ["attribute": "value"],
]
]
3.盡可能將nil判斷寫成if let方式解包
var num : CGFloat?
num = 44
if num != nil{
print(num)
}
8AE147D8-5210-4AF5-81AF-3A40668D61E8.png
var num : CGFloat?
num = 44
if let newNum = num {
print(newNum)
}
6C172988-FFA8-43D9-A1D7-B254F7B5BA4F.png
4.盡量用array.append(data)
,而不是用array+[data]
// Build time: 246.50ms 243.55ms
let systemOptions = [ 7, 14, 30, -1 ]
let systemNames = (0...2).map{ String(format: "%d", systemOptions[$0]) } + [NSLocalizedString("everything", comment: "")]
let count = systemOptions.count
let labelNames = Array(systemNames[0..<count]) + [systemNames.last!]
// Build time: 184.18ms
let systemOptions = [ 7, 14, 30, -1 ]
var systemNames = systemOptions.dropLast().map{ String(format: "%d", $0) }
systemNames.append(NSLocalizedString("everything", comment: ""))
let count = systemOptions.count
var labelNames = Array(systemNames[0..<count])
labelNames.append(systemNames.last!)
5. 盡量不要用三目運(yùn)算,用if else代替三目運(yùn)算
// Build time: 76.09ms 74.51ms
let type = 44
let labelNames = type == 0 ? (1...5).map{ type0ToString($0) } : (0...2).map{type0ToString($0)}
// Build time: 71.96ms 72.48ms
var labelNames: [String]
if type == 0 {
labelNames = (1...5).map{type0ToString($0)}
} else {
labelNames = (0...2).map{type0ToString($0)}
}
6.盡量不要用一些內(nèi)置函數(shù)例如round
/ceil
/floor
等等
let a: CGFloat = 22
let b: CGFloat = 22
let c: CGFloat = 22
let d: CGFloat = 22
let e: CGFloat = 22
// Build time: 19.51ms 18.66ms
// let expansion = a - b - c + round(d * 0.66) + e
// Build time: 15.07ms 13.68ms
let expansion = a - b - c + d * 0.66 + e
6.盡量使用純Swift類型,不要和Objective-C混編
盡可能避免混合地使用Swift類型和NSObject子類钥勋,會(huì)對(duì)性能的提高有所幫助
7.盡量避免無(wú)意義的log,保持好的編碼習(xí)慣
參考文章:Swift性能探索和優(yōu)化分析