美文网首页
Swift.Optional 类型"耗时"

Swift.Optional 类型"耗时"

作者: fd5657ed541e | 来源:发表于2018-05-31 21:37 被阅读16次

    Swift 中 Optional 类型是 Swift 作为类型安全的一大亮点, 有同学(婊哥)讲他在项目中 Optional 类型存在耗时的现象
    我大概的模拟了下:

    let loopCounts = 10000
    let parameters = [
        "name": "Just Lee"
        ] as [String: Any]
    
    func loop() {
     
        let dateBeforePrint = Date()
        /// 循环多次
        (0..<loopCounts).forEach { _ in
            /// 1. 直接赋值
            let _ = parameters
        }
    
        let dateAfterPrint = Date()
    
        print(
            dateAfterPrint.timeIntervalSince1970 - dateBeforePrint.timeIntervalSince1970
        )
    
        let anotherDateBeforePrint = Date()
        /// 循环多次
        (0..<loopCounts).forEach { _ in
            /// 2. Cast to Optional Type
            let _ = parameters as? [String: String]
        }
    
        let anotherDateAfterPrint = Date()
    
        print(
            anotherDateAfterPrint.timeIntervalSince1970 - anotherDateBeforePrint.timeIntervalSince1970
        )
    }
    
    /// 多次输出做比较
    (1..<10).forEach { _ in
        loop()
        print("_______________________")
    }
    
    ====================== Console ======================
    0.00882911682128906
    0.0256490707397461
    _______________________
    0.00412702560424805
    0.0183320045471191
    _______________________
    0.00543999671936035
    0.0246081352233887
    _______________________
    0.00349092483520508
    0.0193371772766113
    _______________________
    0.00730085372924805
    0.0257163047790527
    _______________________
    0.00324916839599609
    0.0262701511383057
    _______________________
    0.00549483299255371
    0.0178658962249756
    _______________________
    0.00488090515136719
    0.0260119438171387
    _______________________
    0.00586795806884766
    0.0198431015014648
    _______________________
    

    可以看到两次耗时时间确实差了几倍
    难道 Optional 类型有这么大的性能隐患吗?

    于是又做了另一个测试

    let anotherParameters: [String: Any]? = parameters
    
    (0..<loopCounts).forEach { _ in
        /// 1. 直接赋值
        let _ = parameters
    }
    
    if let _ = anotherParameters {
          /// 2. Unwrapper
        if let _ = anotherParameters {
            let _ = anotherParameters!
        }
    }
    ====================== Console ======================
    0.00864982604980469
    0.00584006309509277
    _______________________
    0.00604915618896484
    0.00566792488098145
    _______________________
    0.00336909294128418
    0.00624585151672363
    _______________________
    0.00316119194030762
    0.00363922119140625
    _______________________
    0.00303220748901367
    0.00542402267456055
    _______________________
    0.00536394119262695
    0.00592207908630371
    _______________________
    0.00556802749633789
    0.0058138370513916
    _______________________
    0.00321006774902344
    0.00402188301086426
    _______________________
    0.00303983688354492
    0.00353813171386719
    _______________________
    

    可以看出如果只是 Unwrapper 并不会有明显的性能损耗, Optional 本身并不会耗时, 它只是一个 enum 类型的容器, 耗时肯定的是 as? 这个关键字, 它的作用是类型转换 T => Optional<T>
    我们可以模拟下:

    enum WrapperBox<Value> {
        case some(Value)
    }
    
    extension WrapperBox {
        var value: Value {
            switch self {
            case .some(let v): return v
            }
        }
    }
    
    (0..<loopCounts).forEach { _ in
        /// 1. 直接赋值
        let _ = parameters
    }
    
    (0..<loopCounts).forEach { _ in
        /// 2. Cast to WapperBox Type and unwrapper
        let _ = WrapperBox.some(parameters).value
    }
    ====================== Console ======================
    0.00923871994018555
    1.31787824630737
    _______________________
    0.00427675247192383
    1.69343376159668
    _______________________
    0.00520586967468262
    1.22949695587158
    _______________________
    0.00712990760803223
    1.32245421409607
    _______________________
    0.00323200225830078
    1.45691418647766
    _______________________
    0.0070960521697998
    1.44571399688721
    _______________________
    0.00644111633300781
    1.28946876525879
    _______________________
    0.00844192504882812
    1.18692207336426
    _______________________
    0.00496792793273926
    1.24417400360107
    _______________________
    

    耗时差了上百倍

    因此在 someValue as? SomeType 时, 额外的耗时是因为 as? 做类型转换产生的, as as! 也是一样的

    相关文章

      网友评论

          本文标题:Swift.Optional 类型"耗时"

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