0

Xcode 用 equatable 协议编译类的时间太长,这有点奇怪,因为在这种特殊情况下它只是比较原始类型,这应该很容易。

花了 608 毫秒进行类型检查:

    public static func == (lhs: TheModel, rhs: TheModel) -> Bool {
    return lhs.firstName == rhs.firstName &&
        lhs.lastName == rhs.lastName &&
        lhs.maritalStatus == rhs.maritalStatus &&
        lhs.sex == rhs.sex &&
        lhs.birthDate == rhs.birthDate &&
        lhs.nationality == rhs.nationality &&
        lhs.hasAnotherNationality == rhs.hasAnotherNationality &&
        lhs.anotherNationality == rhs.anotherNationality &&
        lhs.nif == rhs.nif &&
        lhs.hasAnotherFiscalAddress == rhs.hasAnotherFiscalAddress &&
        lhs.identificationCard == rhs.identificationCard &&
        lhs.identificationCardNumber == rhs.identificationCardNumber &&
        lhs.identificationCardExpirationDate == rhs.identificationCardExpirationDate &&
        lhs.education == rhs.education &&
        lhs.jobTitle == rhs.jobTitle &&
        lhs.patronalEntity == rhs.patronalEntity &&
        lhs.hasLivesOfIncomes == rhs.hasLivesOfIncomes &&
        lhs.permanentAddressContainer == rhs.permanentAddressContainer &&
        lhs.fiscalAddressContainer == rhs.fiscalAddressContainer &&
        lhs.placeOfBirth == rhs.placeOfBirth &&
        lhs.honorificTitle == rhs.honorificTitle &&
        lhs.userType == rhs.userType &&
        lhs.userTypeDescription == rhs.userTypeDescription &&
        lhs.lastVisitedCountryCode == rhs.lastVisitedCountryCode &&
        lhs.lastUpdate == rhs.lastUpdate
}

PS:我知道这个比较有点矫枉过正,但这只是一个例子。顺便说一句,这是在配备核心 i9、64Gb RAM 和 SSD 的 MacbookPro 中。

更新 - 我做了一些更改并运行了更多测试:

测试 1

向 let 添加显式类型并返回 let:花了 ~450ms

public static func == (lhs: TheModel, rhs: TheModel) -> Bool {
    let extractedExpr: Bool = lhs.firstName == rhs.firstName &&
        lhs.lastName == rhs.lastName &&
        lhs.maritalStatus == rhs.maritalStatus &&
        lhs.sex == rhs.sex &&
        lhs.birthDate == rhs.birthDate &&
        lhs.nationality == rhs.nationality &&
        lhs.hasAnotherNationality == rhs.hasAnotherNationality &&
        lhs.anotherNationality == rhs.anotherNationality &&
        lhs.nif == rhs.nif &&
        lhs.hasAnotherFiscalAddress == rhs.hasAnotherFiscalAddress &&
        lhs.identificationCard == rhs.identificationCard &&
        lhs.identificationCardNumber == rhs.identificationCardNumber &&
        lhs.identificationCardExpirationDate == rhs.identificationCardExpirationDate &&
        lhs.education == rhs.education &&
        lhs.jobTitle == rhs.jobTitle &&
        lhs.patronalEntity == rhs.patronalEntity &&
        lhs.hasLivesOfIncomes == rhs.hasLivesOfIncomes &&
        lhs.permanentAddressContainer == rhs.permanentAddressContainer &&
        lhs.fiscalAddressContainer == rhs.fiscalAddressContainer &&
        lhs.placeOfBirth == rhs.placeOfBirth &&
        lhs.honorificTitle == rhs.honorificTitle &&
        lhs.userType == rhs.userType &&
        lhs.userTypeDescription == rhs.userTypeDescription &&
        lhs.lastVisitedCountryCode == rhs.lastVisitedCountryCode &&
        lhs.lastUpdate == rhs.lastUpdate
    
    return extractedExpr
}

测试 2

为每个案例添加括号:与测试 1 相同的结果!

 let extractedExpr: Bool = (lhs.firstName == rhs.firstName) &&
        (lhs.lastName == rhs.lastName) &&
        (lhs.maritalStatus == rhs.maritalStatus) &&
        (lhs.sex == rhs.sex) &&
        (lhs.birthDate == rhs.birthDate) &&
        (lhs.nationality == rhs.nationality) &&
        (lhs.hasAnotherNationality == rhs.hasAnotherNationality) &&
        (lhs.anotherNationality == rhs.anotherNationality) &&
        (lhs.nif == rhs.nif) &&
        (lhs.hasAnotherFiscalAddress == rhs.hasAnotherFiscalAddress) &&
        (lhs.identificationCard == rhs.identificationCard) &&
        (lhs.identificationCardNumber == rhs.identificationCardNumber) &&
        (lhs.identificationCardExpirationDate == rhs.identificationCardExpirationDate) &&
        (lhs.education == rhs.education) &&
        (lhs.jobTitle == rhs.jobTitle) &&
        (lhs.patronalEntity == rhs.patronalEntity) &&
        (lhs.hasLivesOfIncomes == rhs.hasLivesOfIncomes) &&
        (lhs.permanentAddressContainer == rhs.permanentAddressContainer) &&
        (lhs.fiscalAddressContainer == rhs.fiscalAddressContainer) &&
        (lhs.placeOfBirth == rhs.placeOfBirth) &&
        (lhs.honorificTitle == rhs.honorificTitle) &&
        (lhs.userType == rhs.userType) &&
        (lhs.userTypeDescription == rhs.userTypeDescription) &&
        (lhs.lastVisitedCountryCode == rhs.lastVisitedCountryCode) &&
        (lhs.lastUpdate == rhs.lastUpdate)

    return extractedExpr

测试 3

添加了一个布尔数组作为具有显式类型的 var 并附加每个条件:花了 171ms :)

public static func == (lhs: TheModel, rhs: TheModel) -> Bool {
    var result: [Bool] = [Bool]()

    result.append(lhs.firstName == rhs.firstName)
    result.append(lhs.lastName == rhs.lastName)
    result.append(lhs.maritalStatus == rhs.maritalStatus)
    result.append(lhs.sex == rhs.sex)
    result.append(lhs.birthDate == rhs.birthDate)
    result.append(lhs.nationality == rhs.nationality)
    result.append(lhs.hasAnotherNationality == rhs.hasAnotherNationality)
    result.append(lhs.anotherNationality == rhs.anotherNationality)
    result.append(lhs.nif == rhs.nif)
    result.append(lhs.hasAnotherFiscalAddress == rhs.hasAnotherFiscalAddress)
    result.append(lhs.identificationCard == rhs.identificationCard)
    result.append(lhs.identificationCardNumber == rhs.identificationCardNumber)
    result.append(lhs.identificationCardExpirationDate == rhs.identificationCardExpirationDate)
    result.append(lhs.education == rhs.education)
    result.append(lhs.jobTitle == rhs.jobTitle)
    result.append(lhs.patronalEntity == rhs.patronalEntity)
    result.append(lhs.hasLivesOfIncomes == rhs.hasLivesOfIncomes)
    result.append(lhs.permanentAddressContainer == rhs.permanentAddressContainer)
    result.append(lhs.fiscalAddressContainer == rhs.fiscalAddressContainer)
    result.append(lhs.placeOfBirth == rhs.placeOfBirth)
    result.append(lhs.honorificTitle == rhs.honorificTitle)
    result.append(lhs.userType == rhs.userType)
    result.append(lhs.userTypeDescription == rhs.userTypeDescription)
    result.append(lhs.lastVisitedCountryCode == rhs.lastVisitedCountryCode)
    result.append(lhs.lastUpdate == rhs.lastUpdate)

    return !result.contains(false)
}
4

0 回答 0