如何合并多个数组而不减慢编译器? [英] How to merge multiple Arrays without slowing the compiler down?

查看:128
本文介绍了如何合并多个数组而不减慢编译器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

添加这行代码会使我的编译时间从10秒到3分钟。

Adding this line of code causes my compile time to go from 10 seconds to 3 minutes.

var resultsArray = hashTagParticipantCodes + prefixParticipantCodes + asterixParticipantCodes + attPrefixParticipantCodes + attURLParticipantCodes

将其更改为这将使编译时间恢复正常。 / p>

Changing it to this brings the compile time back down to normal.

var resultsArray = hashTagParticipantCodes
resultsArray += prefixParticipantCodes
resultsArray += asterixParticipantCodes
resultsArray += attPrefixParticipantCodes
resultsArray += attURLParticipantCodes

为什么第一行导致我的编译时间减慢所以这么大,并有一个更优雅的方式来合并这些数组比我发布的5行解决方案?

Why does the first line cause my compile time to slow down so drastically and is there a more elegant way to merge these Arrays than the 5 line solution I've posted?

推荐答案

+ 。每次人们抱怨爆炸性编译时,我问你有链接 + ?它总是是的。这是因为 + 是如此严重的重载。也就是说,我认为这在Xcode 8中,至少在我的快速实验中是显着更好。

It's always +. Every time people complain about explosive compile times, I ask "do you have chained +?" And it's always yes. It's because + is so heavily overloaded. That said, I think this is dramatically better in Xcode 8, at least in my quick experiment.

你可以大大加快这个速度,而不需要 var 通过连接数组而不是添加它们:

You can dramatically speed this up without requiring a var by joining the arrays rather than adding them:

let resultsArray = [hashTagParticipantCodes,
                    prefixParticipantCodes,
                    asterixParticipantCodes, 
                    attPrefixParticipantCodes,
                    attURLParticipantCodes]
                   .joinWithSeparator([]).map{$0}

结尾处的 .map {$ 0} 是强制返回一个数组(如果你需要,否则你可以只使用延迟FlattenCollection)。你也可以这样做:

The .map{$0} at the end is to force it back into an Array (if you need that, otherwise you can just use the lazy FlattenCollection). You can also do it this way:

let resultsArray = Array(
                   [hashTagParticipantCodes,
                    prefixParticipantCodes,
                    asterixParticipantCodes, 
                    attPrefixParticipantCodes,
                    attURLParticipantCodes]
                   .joinWithSeparator([]))

但是检查Xcode 8;我相信这至少是部分固定的(但使用 .joined()仍然快得多,即使在Swift 3)。

But check Xcode 8; I believe this is at least partially fixed (but using .joined() is still much faster, even in Swift 3).

这篇关于如何合并多个数组而不减慢编译器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆