美文网首页Swift学习
探索Swift4项目编译时长

探索Swift4项目编译时长

作者: 船长_ | 来源:发表于2018-02-08 11:59 被阅读50次

    参考原文Swift【优化Swift项目编译时间】
    借助第三方插件
    BuildTimeAnalyzer-for-Xcode

    CA689C72-F143-425B-BD12-A30F2A8B8881.png

    使用方法

    1.下载项目后,运行会弹出一个窗口 303095EE-9603-42B4-8615-594A850ED4B4.png 00BF99B2-0F1C-4D41-BEAA-D8F5317C6882.png

    2.按照上面提示,打开自己需要编译的项目,在build setting中,设置Other Swift Flags为-Xfrontend -debug-time-function-bodies
    然后clean ,编译,然后就看到最上面第一个示例图

    通过参考其他博客,并且亲测各种方法

    减少编译时长的方法

    1.使用自定义或者第三方组件,建议用framework的方式集成到项目中,会减少编译时长
    在网上找了个第三方的下拉刷新,这个第三方没有支持carthage然后自己制作了一个framework进行测试

    直接用原代码无framework
    先clean 清理DerivedData缓存
    1. 21s   2.14s  3. 13s  4. 13s  5. 13s   6. 13s  7. 12s
    排除 第一个偏差大的 
    78/6 = 13
    如果仅仅是clean然后编译 6s  6s  5s 5s  5s 5s 总32 平均 =5.33333
    
    用framework 
    先clean 清理DerivedData缓存 
    1. 16s  2. 12s   3. 12s  4.12s  5.10s  46/4 = 11.5
    仅仅是clean 5s 3s 4s 6s 4s 4s 总26 平均 = 4.33333
    

    2.明确指明类型,不要让编译器去自动推导
    这个结论其实不准确,亲测如下

           // 类型声明之后:  48.89ms 47.78ms     clean之后: 50.38ms 49.00ms
           // 自动推导 45.62  47.56ms  49.19ms   clean之后:  50.21ms 49.23ms
           // : Dictionary<String, Any>
            let myCompany = [
                "employees": [
                    "employee 1": ["attribute": "value"],
                    "employee 2": ["attribute": "value"],
                    "employee 3": ["attribute": "value"],
                    "employee 4": ["attribute": "value"],
                    "employee 5": ["attribute": "value"],
                    "employee 6": ["attribute": "value"],
                    "employee 7": ["attribute": "value"],
                    "employee 8": ["attribute": "value"],
                    "employee 9": ["attribute": "value"],
                    "employee 10": ["attribute": "value"],
                    "employee 11": ["attribute": "value"],
                    "employee 12": ["attribute": "value"],
                    "employee 13": ["attribute": "value"],
                    "employee 14": ["attribute": "value"],
                    "employee 15": ["attribute": "value"],
                    "employee 16": ["attribute": "value"],
                    "employee 17": ["attribute": "value"],
                    "employee 18": ["attribute": "value"],
                    "employee 19": ["attribute": "value"],
                    "employee 20": ["attribute": "value"],
    
                ]
            ]
    

    3.尽可能将nil判断写成if let方式解包

    var num : CGFloat?
    num = 44
    if num != nil{
       print(num)
    }
    
    8AE147D8-5210-4AF5-81AF-3A40668D61E8.png
    var num : CGFloat?
    num = 44
    if let newNum = num {
        print(newNum)
    }
    
    6C172988-FFA8-43D9-A1D7-B254F7B5BA4F.png

    4.尽量用array.append(data),而不是用array+[data]

    // Build time: 246.50ms   243.55ms
    let systemOptions = [ 7, 14, 30, -1 ]
    let systemNames = (0...2).map{ String(format: "%d", systemOptions[$0]) } + [NSLocalizedString("everything", comment: "")]
            
    let count = systemOptions.count
    let labelNames = Array(systemNames[0..<count]) + [systemNames.last!]
    
    // Build time: 184.18ms
    let systemOptions = [ 7, 14, 30, -1 ]
    var systemNames = systemOptions.dropLast().map{ String(format: "%d", $0) }
    systemNames.append(NSLocalizedString("everything", comment: ""))
    
    let count = systemOptions.count
    var labelNames = Array(systemNames[0..<count])
    labelNames.append(systemNames.last!)
    

    5. 尽量不要用三目运算,用if else代替三目运算

    // Build time: 76.09ms  74.51ms
    let type = 44
    let labelNames = type == 0 ? (1...5).map{ type0ToString($0) } : (0...2).map{type0ToString($0)}
    
    
    // Build time: 71.96ms 72.48ms
    var labelNames: [String]
    if type == 0 {
        labelNames = (1...5).map{type0ToString($0)}
    } else {
        labelNames = (0...2).map{type0ToString($0)}
    }
    

    6.尽量不要用一些内置函数例如round/ceil/floor等等

    let a: CGFloat = 22
    let b: CGFloat = 22
    let c: CGFloat = 22
    let d: CGFloat = 22
    let e: CGFloat = 22
    // Build time: 19.51ms 18.66ms
    // let expansion = a - b - c + round(d * 0.66) + e
    
    // Build time: 15.07ms  13.68ms
    let expansion = a - b - c + d * 0.66 + e
    

    6.尽量使用纯Swift类型,不要和Objective-C混编

    尽可能避免混合地使用Swift类型和NSObject子类,会对性能的提高有所帮助
    

    7.尽量避免无意义的log,保持好的编码习惯

    参考文章:Swift性能探索和优化分析

    相关文章

      网友评论

      本文标题:探索Swift4项目编译时长

      本文链接:https://www.haomeiwen.com/subject/njazzxtx.html