Last active
October 3, 2019 11:07
-
-
Save bannzai/90a1d71a52de8973c5fc4c41fa9f49e6 to your computer and use it in GitHub Desktop.
SwiftUI with SwiftDSL vs SwiftUI not SwiftDSL
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import UIKit | |
import SwiftUI | |
class SceneDelegate: UIResponder, UIWindowSceneDelegate { | |
var window: UIWindow? | |
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) { | |
if let windowScene = scene as? UIWindowScene { | |
let window = UIWindow(windowScene: windowScene) | |
self.window = window | |
} | |
window?.rootViewController = UIHostingController(rootView: EmptyView()) | |
window?.makeKeyAndVisible() | |
sleep(1) | |
print("---------- start -------------") | |
let date1 = Date() | |
window?.rootViewController = UIHostingController(rootView: SwiftDSLView()) | |
window?.makeKeyAndVisible() | |
print("SwiftDSLView: \(Date().timeIntervalSince(date1))") | |
sleep(1) | |
let date2 = Date() | |
window?.rootViewController = UIHostingController(rootView: SwiftNoDSLView()) | |
window?.makeKeyAndVisible() | |
print("SwiftNoDSLView: \(Date().timeIntervalSince(date2))") | |
window?.rootViewController = UIHostingController(rootView: EmptyView()) | |
window?.makeKeyAndVisible() | |
print("---------- end -------------") | |
} | |
} | |
import SwiftUI | |
class ViewModel: ObservableObject { | |
@Published var counter = 0 | |
func start() { | |
for i in (1...100000) { | |
counter = i | |
} | |
} | |
} | |
struct SwiftDSLView: View { | |
@ObservedObject(initialValue: ViewModel()) var viewModel | |
var counter: Int { viewModel.counter } | |
var body: some View { | |
Group { | |
if counter % 2 == 0 { | |
Text("\(self.counter)") | |
} else { | |
Text("\(self.counter)") | |
} | |
} | |
.onAppear { | |
self.viewModel.start() | |
} | |
} | |
} | |
struct SwiftNoDSLView: View { | |
@ObservedObject(initialValue: ViewModel()) var viewModel | |
var counter: Int { viewModel.counter } | |
var body: some View { | |
return Group { () -> Text in | |
switch counter % 2 == 0 { | |
case true: | |
return Text("\(counter)") | |
case false: | |
return Text("\(counter)") | |
} | |
} | |
.onAppear { | |
self.viewModel.start() | |
} | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
誤差