Skip to content

Instantly share code, notes, and snippets.

@derekcoder
Forked from waylybaye/Measure.swift
Created May 6, 2020 17:24
Show Gist options
  • Save derekcoder/81e654fa3b26266105b7937649b0fd2d to your computer and use it in GitHub Desktop.
Save derekcoder/81e654fa3b26266105b7937649b0fd2d 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")
}
}
}
@derekcoder
Copy link
Author

Hello

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment