前言
Swift编译时间过长,是件难以忍受的事情。你能忍心看着几行看似简单的代码,慢吞吞的编译吗?╮(╯_╰)╭。下面来介绍几种减少编译时间的方法。
查看每个函数编译时间
若要对症下药,首先要抓住作祟的小鬼。有2种方法。
1、在build setting中,设置Other Swift Flags为-Xfrontend -debug-time-function-bodies。这样编译时清楚的看到每个函数的编译时间,找到耗时较多的地方。
点击右边的展开按钮,可详细查看时间。
2、Xcode plugin,https://github.com/RobertGummesson/BuildTimeAnalyzer-for-Xcode
方法
明确指明类型,不要让编译器去推断
经过测试,发现,在ViewDidLoad中添加如下代码,build的话,在我的机器上一直卡着不动。原因不明,不知道是机器还是xcode问题,还是本身就很慢。但是在原文章说,耗时630ms。总之看起来是比较耗时的。
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"],
]
]
但是将类型声明之后,
let myCompany: Dictionary<String, AnyObject> = ...
ViewDidLoad消耗的时间立马降到十几ms。
nil判断
将nil判断写成if let方式解包。
class CMExpandingTextField: UITextField {
func textFieldEditingChanged() {
invalidateIntrinsicContentSize()
}
override func intrinsicContentSize() -> CGSize {
if isFirstResponder(), let text = text {
let size = text.sizeWithAttributes(typingAttributes)
// Build time: 5238.3ms
return CGSize(width: size.width + (rightView?.bounds.width ?? 0) + (leftView?.bounds.width ?? 0) + 22, height: bounds.height)
// Build time: 32.4ms
var padding: CGFloat = 22
if let rightView = rightView {
padding += rightView.bounds.width
}
if let leftView = leftView {
padding += leftView.bounds.width
}
return CGSizeMake(size.width + padding, bounds.height)
}
return super.intrinsicContentSize()
}
}
array+[]
将array+[data],变成array.append(data)
// Build time: 1250.3ms
let systemOptions = [ 7, 14, 30, -1 ]
let systemNames = (0...2).map{ String(format: localizedFormat, systemOptions[$0]) } + [NSLocalizedString("everything", comment: "")]
// Some code in-between
labelNames = Array(systemNames[0..<count]) + [systemNames.last!]
// Build time: 25.5ms
let systemOptions = [ 7, 14, 30, -1 ]
var systemNames = systemOptions.dropLast().map{ String(format: localizedFormat, $0) }
systemNames.append(NSLocalizedString("everything", comment: ""))
// Some code in-between
labelNames = Array(systemNames[0..<count])
labelNames.append(systemNames.last!)
三段式判断 a > b ? a : b
// Build time: 239.0ms
let labelNames = type == 0 ? (1...5).map{type0ToString($0)} : (0...2).map{type1ToString($0)}
// Build time: 16.9ms
var labelNames: [String]
if type == 0 {
labelNames = (1...5).map{type0ToString($0)}
} else {
labelNames = (0...2).map{type1ToString($0)}
}
CGFloat-->CGFloat类型转换
// Build time: 3431.7 ms
return CGFloat(M_PI) * (CGFloat((hour + hourDelta + CGFloat(minute + minuteDelta) / 60) * 5) - 15) * unit / 180
// Build time: 3.0ms
return CGFloat(M_PI) * ((hour + hourDelta + (minute + minuteDelta) / 60) * 5 - 15) * unit / 180
Round()
// Build time: 1433.7ms
let expansion = a — b — c + round(d * 0.66) + e
// Build time: 34.7ms
let expansion = a — b — c + d * 0.66 + e
总结
减少编译时间归结于几点:
1、明确指明类型,而不要让编译器去推断代码中你所使用的类型,这样会节省大部分的时间。
2、减少让编译器懵逼的代码,尽量写的简单,明了,不要绕圈子。
Whether or not you have a problem with slow build times, it is still useful to build an understanding of what confuses the compiler. I’m sure you’ll find a few surprises yourself
一句话,要善意的对待编译器,不要整各种来虐待它~ o( ̄▽ ̄)ブ~