2017-04-24 2 views
1

내 응용 프로그램 내부에 일부보기를 표시하기 위해 디버깅 목적으로 사용하는보기 컨트롤러가 있습니다 (로컬 xcode 빌드에만 해당, 응용 프로그램 저장소 버전에는이 컨트롤러가 없음).UILabel.attributedText를 동일한 값으로 설정하는 데 비용이 많이 듭니까?

이 컨트롤러에는 내부 구성 요소의 상태를 반영하고자하는 레이블이 있습니다 (특히 해당 구성 요소의 활성화 또는 비활성화 여부를 표시해야 함).

내 질문 :

# 1 : 그것은 예전으로 비싼 같은 값으로 UILabel의의 .attributedText 속성을 설정하는 것입니다, 아니면 이전 값을 캐시해야하고 변경하는 경우에만 속성을 설정 ?

# 2 : .text (속성이 아닌) 속성은 무엇입니까?

나는 현재 다음과 같은 코드를 사용하고 있습니다 : 나는 몇 가지 숫자를 공유하기 전에

// Schedule timer to update the control panel. (This is debug-only, so not worth 
// the complexity of making this event-based) 
Timer.scheduledTimer(withTimeInterval: 0.5, 
       repeats: true) { [weak self] timer in 

    DispatchQueue.main.async { 

     // Stop timer if we've been dealloced or are no longer being presented 
     guard let strongSelf = self, 
       strongSelf.isBeingPresented else 
     { 
      timer.invalidate() 
      return 
     } 

     // "Something Enabled/Disabled" label 
     let somethingIsEnabled = strongSelf.someDepenency.isEnabled 
     let somethingEnabledString = NSMutableAttributedString(string: "Something ") 

     somethingEnabledString.append(NSAttributedString(string: isEnabled ? "Enabled" : "Disabled", 
                 attributes: isEnabled ? nil : [NSForegroundColorAttributeName: UIColor(xtHardcodedHexValue: "0xCD0408")])) 
     strongSelf.somethingEnabledLabel?.attributedText = somethingEnabledString 

    } 
} 
+2

실제로 성능 문제가 있습니까? 이 질문에 대한 진정한 요점은없는 것 같습니다. – rmaddy

+0

글쎄, 내 팀원 중 일부는 (CR 동안) 걱정하고있다. 당신 말이 맞아요, 그것은 대부분 호기심이고 UIKit에 대한 이해를 높이기 위해서입니다. 라벨이 초당 60x를 업데이트해야한다면 어떨까요? 'attributedText'라는 레이블을 100 개나 가지고 있다면 어떨까요? – Julien

+1

테스트합니다. 성과를 측정합니다. 실제로 문제가있는 경우에만 그것에 대해 아무 것도 시도하지 않습니다. – rmaddy

답변

3

을, 나는 매우는 조기 최적화를 수행 할 것을 권장합니다 을 것입니다. 다음 코드를 고려 : 여기

private func getAttributedString() -> NSMutableAttributedString{ let attributedString = NSMutableAttributedString(string: "Something ") attributedString.append(NSAttributedString(string: "Enabled", attributes: [NSForegroundColorAttributeName: UIColor(rgb: 0xCD0408)])) return attributedString } //overwrites attributed text 100000 times @IBAction func overwriteAttributedText(_ sender: Any) { let timeBeforeAction = Date.init() print ("Time taken to overwrite attributed text is ") for _ in 1 ... 100000{ label.attributedText = getAttributedString() } let timeAfterAction = Date.init() let timeTaken = timeAfterAction.timeIntervalSince(timeBeforeAction) print(timeTaken) } //overwrites attributed text 100 times @IBAction func cacheAttributedText(_ sender: Any) { let timeBeforeAction = Date.init() print ("Time taken to selectively overwrite attributed text is ") for i in 1 ... 100000{ if i % 1000 == 0 { label.attributedText = getAttributedString() } } let timeAfterAction = Date.init() let timeTaken = timeAfterAction.timeIntervalSince(timeBeforeAction) print(timeTaken) } //overwrites text 100000 times @IBAction func overWriteText(_ sender: Any) { let defaultText = "Hello World" let timeBeforeAction = Date.init() print ("Time taken to overwrite text is ") for _ in 1 ... 100000{ label.text = defaultText } let timeAfterAction = Date.init() let timeTaken = timeAfterAction.timeIntervalSince(timeBeforeAction) print(timeTaken) } 

이 결과는 다음과 같습니다

Time taken to overwrite attributed text is 0.597925961017609 
Time taken to selectively overwrite attributed text is 0.004891037940979 
Time taken to overwrite text is 0.0462920069694519 

결과는 스스로를 이야기하지만, 이러한 최적화 심지어 필요한 경우 내가 당신을 떠나지.

+0

번호 주셔서 감사합니다. 당신이 말했듯이, 조기 최적화의 가치는 없지만 성능 차이를보기에는 꽤 흥미 롭습니다. – Julien