728x90
n이 커질 수록 append가 속도 면에서 더 빨라지는 추세
import SwiftUI
struct ContentView: View {
@State var count = 1
var body: some View {
VStack {
Button("count up") {
count *= 10
}
Button("act") {
print("count:", count)
DispatchQueue.global(qos: .default).async {
var array1: [Int] = []
measure(label: "addTest", repeatCount: count) {
addTest(array: &array1)
}
}
DispatchQueue.global(qos: .default).async {
var array2: [Int] = []
measure(label: "appendTest", repeatCount: count) {
appendTest(array: &array2)
}
}
}
}
}
}
func addTest(array: inout [Int]) { array += [1] }
func appendTest(array: inout [Int]) { array.append(1) }
func measure(label: String, repeatCount: Int, function: () -> ()) {
let time = ContinuousClock().measure { for _ in 1...repeatCount { function() } }
print(label, ":", time)
}
count: 1
addTest : 3.8833e-05 seconds
appendTest : 3.9833e-05 seconds
count: 10
appendTest : 1.3625e-05 seconds
addTest : 2.3042e-05 seconds
count: 100
addTest : 0.000171959 seconds
appendTest : 6.8292e-05 seconds
count: 1000
appendTest : 0.00031825 seconds
addTest : 0.000650166 seconds
count: 10000
appendTest : 0.003383875 seconds
addTest : 0.005933375 seconds
count: 100000
appendTest : 0.033002375 seconds
addTest : 0.057534667 seconds
count: 1000000
appendTest : 0.1975555 seconds
addTest : 0.372229417 seconds
count: 10000000
appendTest : 1.748122291 seconds
addTest : 3.475922875 seconds
count: 100000000
appendTest : 17.335491125 seconds
addTest : 34.491072791 seconds
count: 1000000000
appendTest : 178.26301329199998 seconds
addTest : 352.920505958 seconds
728x90
'Swift' 카테고리의 다른 글
주니어 iOS 개발자 Swift 면접 질문 (0) | 2025.04.23 |
---|---|
Swift: add, adds, append, appends 속도 비교 (0) | 2025.03.30 |
Swift: if let, guard let 속도 비교 (0) | 2025.03.30 |
Swift: async let 수행 순서 (0) | 2025.03.30 |
Swift Concurrency (0) | 2025.02.04 |