在Xcode中以特定方法缩短编译时间

时间:2017-06-29 02:50:41

标签: swift xcode

我正在使用Xcode 8.2.1,我的上一个项目的编译时间非常慢。我花了一些时间研究为什么会这样,我开始跟踪项目的编译时间。所以主要问题似乎在这里:

  

49,609.4ms来编译moveIcons()

现在这对我来说有点意外,因为moveIcons()并不是一个非常广泛的方法:

func moveIcons(_ motion : SlideMotion) {
    var direction : CGFloat = 1
    if motion == .Out {
        direction = -1
    }
    for icon in 0...3 {
        let currentIcon = pauseIcons[icon]
        var moveAction = SKAction.sequence([SKAction.move(by: CGVector(dx: (-scene.size.width * direction * 0.26) - (scene.size.width * 0.18 * direction), dy: 0), duration: 0.2), SKAction.move(by: CGVector(dx: scene.size.width * direction * 0.18, dy: 0), duration: 0.1)])
        if icon == 3 {
            moveAction = SKAction.sequence([SKAction.move(by: CGVector(dx: -scene.size.width * direction, dy: 0), duration: 0.3), SKAction.move(by: CGVector(dx: scene.size.width * direction * 0.25, dy: 0), duration: 0.15)])
        }
        currentIcon.run(moveAction)
    }
}

* pauseIcons初始化如下:(它可能像4个对象一样)

var pauseIcons : [SKSpriteNode] = []

现在我想也许问题是因为类型推断正在减慢构建速度。因此,我这样做了:

func moveIcons(_ motion : SlideMotion) {
    var direction : CGFloat = 1
    if motion == .Out {
        direction = -1
    }
    for icon in 0...3 {
        let currentIcon : SKSpriteNode = pauseIcons[icon]
        var moveAction : SKAction = SKAction.sequence([SKAction.move(by: CGVector(dx: (-scene.size.width * direction * 0.26) - (scene.size.width * 0.18 * direction), dy: 0), duration: 0.2), SKAction.move(by: CGVector(dx: scene.size.width * direction * 0.18, dy: 0), duration: 0.1)])
        if icon == 3 {
            moveAction = SKAction.sequence([SKAction.move(by: CGVector(dx: -scene.size.width * direction, dy: 0), duration: 0.3), SKAction.move(by: CGVector(dx: scene.size.width * direction * 0.25, dy: 0), duration: 0.15)])
        }
        currentIcon.run(moveAction)
    }
}

然而,编译时间仍然是我项目中最高的:

enter image description here

有没有人知道为什么这个特定方法需要花费这么多时间来编译?

1 个答案:

答案 0 :(得分:2)

这个表达式和相关表达式非常复杂:

var moveAction = SKAction.sequence([SKAction.move(by: CGVector(dx: (-scene.size.width * direction * 0.26) - (scene.size.width * 0.18 * direction), dy: 0), duration: 0.2), SKAction.move(by: CGVector(dx: scene.size.width * direction * 0.18, dy: 0), duration: 0.1)])

用中间变量打破这些。问题不在于找出最终的类型。问题是找出*-的所有可能性。是的,只有一个有效的可能性,但编译器必须检查每个可能的类型组合,以确保表达式不含糊。特别是,它必须做很多工作来决定0.26CGFloatFloat还是Double

更多内容如下:

for icon in 0...3 {
    let currentIcon : SKSpriteNode = pauseIcons[icon]

    let moveAction: SKAction

    if icon == 3 {
        let moveBy1 = CGVector(dx: -scene.size.width * direction, dy: 0)
        let moveBy2 = CGVector(dx: scene.size.width * direction * 0.25, dy: 0)
        moveAction = SKAction.sequence([
            SKAction.move(by: moveBy1), duration: 0.3),
            SKAction.move(by: moveBy2, duration: 0.15)])
    } else {
        let moveBy1 = CGVector(dx: (-scene.size.width * direction * 0.26) - (scene.size.width * 0.18 * direction), dy: 0)
        let moveBy2 = CGVector(dx: scene.size.width * direction * 0.18, dy: 0)
        moveAction = SKAction.sequence([SKAction.move(by: moveBy1, duration: 0.2),
                                        SKAction.move(by: moveBy2, duration: 0.1)])
    }
    currentIcon.run(moveAction)
}