λ³Έλ¬Έ λ°”λ‘œκ°€κΈ°

🍎 iOS & Swift

νƒ€μž… μΆ”λ‘ κ³Ό νƒ€μž… μ–΄λ…Έν…Œμ΄μ…˜μ˜ 속도 차이에 κ΄€ν•˜μ—¬(🧐 νƒ€μž… 좔둠이 더 λΉ λ₯΄λ‹€?)

κ·Έ λ™μ•ˆ λ§‰μ—°ν•˜κ²Œλ„ νƒ€μž… μ–΄λ…Έν…Œμ΄μ…˜(λͺ…μ‹œ)κ°€ 컴파일 속도가 더 λΉ λ₯΄λ‹€κ³  μƒκ°ν–ˆλ˜μ§€λΌ 아무 생각 없이 νƒ€μž… λͺ…μ‹œλ₯Ό ν•˜λŠ” μŠ΅κ΄€μ„ 듀이고 μžˆμ—ˆλ‹€. κ·ΈλŸ¬λ‚˜ 졜근 λ“£κ³  μžˆλŠ” ꡐ윑 κ³Όμ •μ—μ„œ λ©˜ν† λ‹˜κ»˜μ„œ νƒ€μž… 좔둠을 ν•˜λŠ” 것이 더 λ‚˜μ•„λ³΄μΈλ‹€λΌλŠ” 말씀을 ν•΄μ£Όμ…¨κ³ , μ™œμΈμ§€ κΆκΈˆν–ˆλ˜μ§€λΌ μ—¬μ­€λ³΄κ²Œ λ˜μ—ˆλ‹€. λ‹΅λ³€ μ£Όμ‹  κ²ƒμœΌλ‘œλŠ” 컴파일러의 μ„±λŠ₯이 점점 더 κ°œμ„ λ˜κ³  있고, νƒ€μž… 좔둠이 컴파일 속도가 더 λΉ λ₯΄λ‹€λŠ” μ‹€ν—˜ 결과도 μžˆλ‹€κ³  λ‹΅λ³€μ£Όμ…¨λ‹€.

 

μ‹€μ œλ‘œ μ•„λž˜μ— μ²¨λΆ€ν•œ 링크λ₯Ό 보면 κ°„λ‹¨ν•œ μ‹€ν—˜ κ²°κ³Όλ₯Ό 확인해볼 수 μžˆλ‹€.

https://forums.swift.org/t/regarding-swift-type-inference-compile-time-performance/49748/2

let a = "hello, world!" // type is inferred
let b = String("hello, world!") // type is inferred from String(...) and then passed to the root (the constant b)
let c: String = .init("hello, world!") // type inference is not required
let d: String = "hello, world!" // type inference is not required

κ²°κ³Όλ₯Ό 보면 1번(νƒ€μž… μΆ”λ‘ )을 ν•œ 것이 속도가 κ°€μž₯ λΉ¨λžλ‹€.

Benchmark #1: xcrun swiftc -typecheck a.swift
  Time (mean ± σ):     175.7 ms ±   3.5 ms    [User: 82.9 ms, System: 81.9 ms]
  Range (min … max):   171.0 ms … 182.8 ms    16 runs

Benchmark #1: xcrun swiftc -typecheck b.swift
  Time (mean ± σ):     224.8 ms ±   2.8 ms    [User: 131.1 ms, System: 81.7 ms]
  Range (min … max):   220.2 ms … 228.2 ms    13 runs

Benchmark #1: xcrun swiftc -typecheck c.swift
  Time (mean ± σ):     672.3 ms ±   8.0 ms    [User: 568.3 ms, System: 93.7 ms]
  Range (min … max):   662.4 ms … 685.1 ms    10 runs

Benchmark #1: xcrun swiftc -typecheck d.swift
  Time (mean ± σ):     213.3 ms ±   2.0 ms    [User: 119.8 ms, System: 81.6 ms]
  Range (min … max):   210.2 ms … 216.5 ms    13 runs

λ‚˜λŠ” νƒ€μž…μ„ μΆ”λ‘ ν•˜λŠ”λ° 더 λ§Žμ€ λ¦¬μ†ŒμŠ€κ°€ λ“€ 것이라고 κ·Έλƒ₯ μƒκ°ν•˜κ³  μžˆμ—ˆλŠ”λ° νƒ€μž… 좔둠이 속도가 더 λΉ λ₯΄λ‹€λ‹ˆ... λ¬Όλ‘  μ‹€ν—˜ κ²°κ³Ό 쀑 1가지이긴 ν•˜μ§€λ§Œ 되게 μ‹ κΈ°ν–ˆλ‹€. λ§λΆ™μ—¬μ„œ λ©˜ν† λ‹˜κ»˜μ„œλŠ” νƒ€μž…을 λͺ…μ‹œν•˜κ²Œ 되면 μ»΄νŒŒμΌλŸ¬κ°€ λͺ…μ‹œλœ νƒ€μž…κ³Ό μ΄ˆκΉƒκ°’μ„ λΉ„κ΅ν•˜λŠ” μž‘μ—…μ΄ μΆ”κ°€λ˜μ–΄μ„œ 더 느리고, μ• μ΄ˆμ— νƒ€μž… λͺ…μ‹œμ™€ νƒ€μž… μΆ”λ‘  속도 차이 μžμ²΄λ„ λ°€λ¦¬μ„Έμ»¨λ“œ λ‹¨μœ„ 차이둜 λ―Έμ„Έν•˜λ‹€λΌκ³  말씀 ν•΄μ£Όμ…¨λ‹€.

 

컴파일 μ„±λŠ₯ μžμ²΄μ— 영ν–₯을 크게 μ£ΌλŠ” 것이 μ•„λ‹ˆλΌλ©΄, νƒ€μž… λͺ…μ‹œλ₯Ό ν•˜λ‚˜ν•˜λ‚˜ ν•΄μ£ΌλŠ” 것은 정말 λΉ„νš¨μœ¨μ μΈ μž‘μ—…μΌ 것이닀. νƒ€μž… 좔둠을 ν•˜λŠ” λ°©ν–₯으둜 μ½”λ“œ μž‘μ„± μŠ΅κ΄€μ„ 바꾸도둝 ν•΄μ•Όκ² λ‹€. 그리고 ν™•μ‹€ν•œ κ·Όκ±° 없이 λ§‰μ—°νžˆ 무엇이 더 쒋겠닀라고 μƒκ°ν–ˆλ˜ λ‚˜ μžμ‹ μ„ λ°˜μ„±ν•˜κ³ μž ν•œλ‹€.