添加这行代码会使我的编译时间从 10 秒缩短到 3 分钟。
var resultsArray = hashTagParticipantCodes + prefixParticipantCodes + asterixParticipantCodes + attPrefixParticipantCodes + attURLParticipantCodes
将其更改为这样会使编译时间恢复正常。
var resultsArray = hashTagParticipantCodes
resultsArray += prefixParticipantCodes
resultsArray += asterixParticipantCodes
resultsArray += attPrefixParticipantCodes
resultsArray += attURLParticipantCodes
为什么第一行会导致我的编译时间大幅减慢,是否有比我发布的 5 行解决方案更优雅的方法来合并这些数组?
总是如此+
。每当人们抱怨爆炸性的编译时间时,我都会问“你有链接吗?+
?”而且总是肯定的。这是因为+
如此严重超载。也就是说,我认为 Xcode 8 的表现要好得多,至少在我的快速实验中是这样。
您可以显着加快速度,而无需var
通过连接数组而不是添加它们:
let resultsArray = [hashTagParticipantCodes,
prefixParticipantCodes,
asterixParticipantCodes,
attPrefixParticipantCodes,
attURLParticipantCodes]
.joinWithSeparator([]).map{$0}
The .map{$0}
最后是将其强制放回到数组中(如果您需要的话,否则您可以使用惰性的 FlattenCollection)。您也可以这样做:
let resultsArray = Array(
[hashTagParticipantCodes,
prefixParticipantCodes,
asterixParticipantCodes,
attPrefixParticipantCodes,
attURLParticipantCodes]
.joinWithSeparator([]))
但检查 Xcode 8;我相信这至少是部分固定的(但使用.joined()
即使在 Swift 3 中,仍然要快得多)。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)