【问题标题】:Is accessing objects frequently using Computed Properties affecting performance?频繁使用计算属性访问对象会影响性能吗?
【发布时间】:2016-05-10 03:08:05
【问题描述】:

我正在使用 GameplayKit 和 SpriteKit 框架编写游戏。在 Apple 关于 GameplayKit 的示例中,您经常会看到以下内容:

class PlayerEntity: GKEntity {
    // MARK: Components

    var renderComponent: RPRenderComponent {
        guard let renderComponent = componentForClass(RPRenderComponent.self) else {
            fatalError()
        }
        return renderComponent
    }
    var stateMachineComponent: RPStateMachineComponent {
        guard let stateMachineComponent = componentForClass(RPStateMachineComponent.self) else {
            fatalError()
        }
        return stateMachineComponent
    }
    // MARK: Initialisation
    override init() {
        super.init()

        let renderComponent = RPRenderComponent()
        renderComponent.node.entity = self;
        let stateMachineComponent = RPStateMachineComponent(states: [
            RPPlayerStandingState(entity: self),
            RPPlayerFallingState(entity: self),
            RPPlayerBouncingDownState(entity: self),
            RPPlayerBouncingUpState(entity: self),
            RPPlayerJumpingState(entity: self),
            RPPlayerBoostState(entity: self)
            ])
        addComponent(renderComponent)
        addComponent(stateMachineComponent)
    }
}

组件在它们所属的类的初始化过程中被创建和初始化,并通过addComponent(component: GKComponent)添加到组件数组中。

为了使这些组件可以从类外部访问,Apple 示例始终使用调用 componentForClass() 的计算属性来返回相应的组件实例。

然而,渲染组件是“每帧”访问的,这意味着在每个更新周期中我都需要调用渲染组件,这将导致调用计算属性,这在我看来会导致额外且可避免的处理负载。

调用看起来像:

func update(withDeltaTime time: NSTimeInterval) {
    playerEntity.renderNode.DoSomethingPerFrame()
}

而不是这个,我这样做如下:

class PlayerEntity: GKEntity {
    // MARK: Components

    let renderComponent: RPRenderComponent
    var stateMachineComponent: RPStateMachineComponent!

    // MARK: Initialisation
    override init() {
        renderComponent = RPRenderComponent()
        super.init()
        renderComponent.node.entity = self

        stateMachineComponent = RPStateMachineComponent(states: [
            RPPlayerStandingState(entity: self),
            RPPlayerFallingState(entity: self),
            RPPlayerBouncingDownState(entity: self),
            RPPlayerBouncingUpState(entity: self),
            RPPlayerJumpingState(entity: self),
            RPPlayerBoostState(entity: self)
            ])
        addComponent(renderComponent)
        addComponent(stateMachineComponent)

    }
}

我没有使用计算属性访问组件,而是在我的类中持有对它的强引用。在我看来,我在调用计算属性时避免了额外的开销,因为我避免了“计算”。

但我不太清楚为什么 Apple 使用计算属性来做到这一点。也许我对计算属性完全错误,或者只是因为这是编写该示例的人的编码风格!?

计算属性会影响性能吗?它们是否自动意味着更多的开销?

或者:

使用这样的计算属性是否可以且“安全”(就资源而言)? (在我看来,计算属性尽管害怕一个相当优雅的解决方案,顺便说一句。)

【问题讨论】:

    标签: ios swift properties


    【解决方案1】:

    这取决于 - 每次计算房产成本的多少,以及您计算的频率。

    一般来说,如果计算成本太低以至于影响不大,则应使用计算属性,如果需要更长的时间,则应使用函数。或者,如果计算属性经常返回相同的结果,则计算属性可以缓存其值。

    使用得当,所涉及的成本非常低。您还需要建议一个易于使用且速度更快的计算属性的替代方案。

    【讨论】:

    • 计算属性如何缓存它返回的结果,顺便说一句?而且“正确使用”与我的代码有点相关,因为我真的不知道底层 GameplayKit 的作用......
    猜你喜欢
    • 2016-11-27
    • 1970-01-01
    • 2021-12-29
    • 1970-01-01
    • 2020-04-19
    • 2011-07-07
    • 1970-01-01
    • 2021-04-18
    • 2013-12-31
    相关资源
    最近更新 更多