Static vs class functions/variables in Swift classes?

前端 未结 9 1047
不知归路
不知归路 2020-11-28 17:13

The following code compiles in Swift 1.2:

class myClass {
    static func myMethod1() {
    }
    class func myMethod2() {
    }
    static var myVar1 = \"\"         


        
9条回答
  •  悲哀的现实
    2020-11-28 18:15

    Testing in Swift 4 shows performance difference in simulator. I made a class with "class func" and struct with "static func" and ran them in test.

    static func is:

    • 20% faster without compiler optimization
    • 38% faster when optimization -whole-module-optimization is enabled.

    However, running the same code on iPhone 7 under iOS 10.3 shows exactly the same performance.

    Here is sample project in Swift 4 for Xcode 9 if you like to test yourself https://github.com/protyagov/StructVsClassPerformance

提交回复
热议问题