Skip to content

Instantly share code, notes, and snippets.

@waylybaye
Last active March 12, 2024 14:09
Show Gist options
  • Star 22 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save waylybaye/7d7a08cbfca0654f24883178578a3ce8 to your computer and use it in GitHub Desktop.
Save waylybaye/7d7a08cbfca0654f24883178578a3ce8 to your computer and use it in GitHub Desktop.
SwiftUI view render performance Indicator
// Created by Baye Wayly on 2020/3/13.
// Copyright © 2020 Baye. All rights reserved.
import SwiftUI
struct Measure<Content: View>: View {
@State var cost: TimeInterval = 0
var content: Content
init(@ViewBuilder builder: () -> Content) {
// discard first time
content = builder()
let start = Date()
let count = 50
for _ in 0..<count {
content = builder()
}
self._cost = State(initialValue: Date().timeIntervalSince(start) / Double(count))
}
var body: some View {
ZStack(alignment: .topLeading) {
self.content
Text("\(Int(self.cost * 1000_1000))μs")
.font(.system(.caption, design: .monospaced))
.padding(.vertical, 3)
.padding(.horizontal, 5)
.background(Color.orange)
.foregroundColor(.white)
.clipShape(RoundedRectangle(cornerRadius: 3))
}
}
}
struct Measure_Previews: PreviewProvider {
static var previews: some View {
Measure {
Text("Hello World")
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment