识别慢代码以优化构建时间 [英] Identify slow code to optimize build time

查看:37
本文介绍了识别慢代码以优化构建时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用这些编译 Swift 标志来识别减慢编译时间的代码:

-Xfrontend -warn-long-function-bodies=100-Xfrontend -warn-long-expression-type-checking=100

然后在构建之后,我得到如下warnings:

实例方法 'startFadePositionTitle()' 花费 2702 毫秒进行类型检查(限制:500 毫秒)

对于这部分代码:

 func startFadePositionTitle() ->CGFloat {让值:CGFloat = ((backgroundImage.frame.height/2 - contentTitle.frame.height/2) - navbarView.frame.height)/2返回值}

有人能解释一下这种方法有什么问题吗?我可以改进什么?

解决方案

你应该把它分成更小的块,这样 Swift 就可以更轻松地进行类型检查.而且你说的越多,Swift 就越不需要思考.所以你可以帮助编译器并告诉它你已经知道的任何事情:

func beginFadePositionTitle() ->CGFloat {让 n: CGFloat = 2让一个:CGFloat = self.backgroundImage.frame.height/n让 b: CGFloat = self.contentTitle.frame.height/n让 ab:CGFloat = a - b让 c: CGFloat = self.navbarView.frame.height让 abc: CGFloat = ab - c返回 abc/n}

<块引用>

实例方法 'beginFadePositionTitle()' 花费 1 毫秒 进行类型检查(限制:1 毫秒)

这是将所有内容都告诉编译器时的结果.看到区别了吗?

I'm using these Compilation Swift Flag to identify codes that slow down the compilation time:

-Xfrontend -warn-long-function-bodies=100
-Xfrontend -warn-long-expression-type-checking=100

Then after building, I get warnings like these:

Instance method 'startFadePositionTitle()' took 2702ms to type-check (limit: 500ms)

for this part of the code:

    func startFadePositionTitle() -> CGFloat {
        let value: CGFloat = ((backgroundImage.frame.height/2 - contentTitle.frame.height/2) - navbarView.frame.height)/2
        return value
    }

Can someone explains me what is wrong in this method and what could I possibly improve?

解决方案

You should break it to smaller chunks, then Swift can do type check more easily. Also the more you tell, the less Swift has to think. So you can help compiler and tell it anything you already know:

func beginFadePositionTitle() -> CGFloat {
    let n: CGFloat = 2
    let a: CGFloat = self.backgroundImage.frame.height/n
    let b: CGFloat = self.contentTitle.frame.height/n
    let ab: CGFloat = a - b
    let c: CGFloat = self.navbarView.frame.height
    let abc: CGFloat = ab - c
    return abc/n
}

Instance method 'beginFadePositionTitle()' took 1ms to type-check (limit: 1ms)

This is the result when you tell everything to compiler. See the difference?

这篇关于识别慢代码以优化构建时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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