Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Swiftプログラマのための今さら聞けない計算量の話
Search
Hisakuni Fujimoto
September 16, 2017
Programming
3.2k
1
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
Swiftプログラマのための今さら聞けない計算量の話
もっさりアプリをサクサクにするための基本の「キ」
Hisakuni Fujimoto
September 16, 2017
Other Decks in Programming
See All in Programming
jsmini JavaScript Engine を作ってみた話
yosuke_furukawa
PRO
0
330
【デモ】Kiroで体験する仕様駆動開発|設計からコーディングまでAIと進める開発フロー
cmkudo
0
110
ここ半年くらいでAIに作らせたR用ツール
eitsupi
0
390
生成AIで帳票OCRが「簡単に」作れる時代になった?
kon_shou
0
1.1k
ドリフトを絶対に許さない(?)CDK運用 / CDK Ops with Zero Tolerance for Drifts (?)
akihisaikeda
1
210
わからない話を追いかけたら、プログラミング言語を作る側にいた
ydah
3
520
Laravelで学ぶ Webアプリケーションチューニング入門/web_application_tuning_101
hanhan1978
4
1.9k
freee が目指す データ マネジメント戦略 AI-Ready 時代を支える 攻めのガバナンスとは
freee
PRO
0
430
実装をデザインガイドラインに追従させるための取り組み / 260731-dip-mosh-design-system
dachi023
0
5.2k
夏だ!祭りだ!祭りとはドメインモデリングでは?
ryugen04
0
320
the container ship “Apple Silicon”@WWDC26 Recap -Japan-\(region).swift
shingangan
0
130
自動化したのに回らないテスト運用の壁ーAI時代の品質責任と生産性
mfunaki
1
140
Featured
See All Featured
Technical Leadership for Architectural Decision Making
baasie
3
500
The Invisible Side of Design
smashingmag
301
52k
The Mindset for Success: Future Career Progression
greggifford
PRO
0
450
SEO Brein meetup: CTRL+C is not how to scale international SEO
lindahogenes
1
2.8k
Facilitating Awesome Meetings
lara
57
7.1k
The Cult of Friendly URLs
andyhume
79
7k
RailsConf & Balkan Ruby 2019: The Past, Present, and Future of Rails at GitHub
eileencodes
141
35k
Kristin Tynski - Automating Marketing Tasks With AI
techseoconnect
PRO
0
470
New Earth Scene 8
popppiees
3
2.5k
Distributed Sagas: A Protocol for Coordinating Microservices
caitiem20
333
23k
How to audit for AI Accessibility on your Front & Back End
davetheseo
0
500
brightonSEO & MeasureFest 2025 - Christian Goodrich - Winning strategies for Black Friday CRO & PPC
cargoodrich
3
770
Transcript
SwiftϓϩάϥϚͷͨΊͷ ࠓ͞Βฉ͚ͳ͍ܭࢉྔͷ ͬ͞ΓΞϓϦΛαΫαΫʹ͢ΔͨΊͷجຊͷʮΩʯ SwiftϓϩάϥϚͷͨΊͷࠓ͞Βฉ͚ͳ͍ܭࢉྔͷ / iOSDC Japan 2017 / ©
Hisakuni Fujimoto, 2017 1
ࣗݾհ • ౻ຊঘ (53ࡀɺ1964ੜ) • ϑϦʔϥϯεϓϩάϥϚ • fhisa (Twitter, GitHub
ͳͲ) • RubyCocoa(2001-), SwiftyLayout(2015-) • Cocoaษڧձ (Bukuro.swiftɺؔ౦swiftษڧձ) SwiftϓϩάϥϚͷͨΊͷࠓ͞Βฉ͚ͳ͍ܭࢉྔͷ / iOSDC Japan 2017 / © Hisakuni Fujimoto, 2017 2
ΞδΣϯμ • ܭࢉྔೖ • Swift Standard Library ͱܭࢉྔ • ·ͱΊ
SwiftϓϩάϥϚͷͨΊͷࠓ͞Βฉ͚ͳ͍ܭࢉྔͷ / iOSDC Japan 2017 / © Hisakuni Fujimoto, 2017 3
ܭࢉྔೖ SwiftϓϩάϥϚͷͨΊͷࠓ͞Βฉ͚ͳ͍ܭࢉྔͷ / iOSDC Japan 2017 / © Hisakuni Fujimoto,
2017 4
ܭࢉྔೖ Complexity: O(1) if the collection conforms to RandomAccessCollection; otherwise,
O(n), where n is the length of the collection. — countϓϩύςΟͷυΩϡϝϯτΑΓൈਮ SwiftϓϩάϥϚͷͨΊͷࠓ͞Βฉ͚ͳ͍ܭࢉྔͷ / iOSDC Japan 2017 / © Hisakuni Fujimoto, 2017 5
ܭࢉྔೖ Complexity: O(1) if the collection conforms to RandomAccessCollection; otherwise,
O(n), where n is the length of the collection. O(1) O(n) ʮܭࢉྔʯ ΞϧΰϦζϜͷෳࡶ͞Λද͢ࢦඪ SwiftϓϩάϥϚͷͨΊͷࠓ͞Βฉ͚ͳ͍ܭࢉྔͷ / iOSDC Japan 2017 / © Hisakuni Fujimoto, 2017 6
ΞϧΰϦζϜͱʁ Λղͨ͘Ίͷखॱ SwiftϓϩάϥϚͷͨΊͷࠓ͞Βฉ͚ͳ͍ܭࢉྔͷ / iOSDC Japan 2017 / © Hisakuni
Fujimoto, 2017 7
ΞϧΰϦζϜͱʁ • ࢁͷσʔλ͔ΒಛఆσʔλΛݟ͚ͭΔํ๏ (ೋ୳ࡧͳͲ) • ࢁͷσʔλΛॱʹฒΔํ๏ (ϚʔδιʔτͳͲ) • ࠷େެΛٻΊΔํ๏ (ϢʔΫϦουͷޓআ๏)
• ύζϧΛղ͘ํ๏ (ϋϊΠͷౝͳͲ) SwiftϓϩάϥϚͷͨΊͷࠓ͞Βฉ͚ͳ͍ܭࢉྔͷ / iOSDC Japan 2017 / © Hisakuni Fujimoto, 2017 8
ܭࢉྔ(Complexity)ͱʁ ͋ΔΞϧΰϦζϜΛ࣮ߦ͢Δͱ͖ɺೖྗͷେ͖͞ʹରͯ͠ɺͲͷ ͘Β͍ͷ࣌ؒ·ͨ(ϝϞϦʔ)ۭ͕ؒඞཁ͔Λද͢ࢦඪɾ҆ SwiftϓϩάϥϚͷͨΊͷࠓ͞Βฉ͚ͳ͍ܭࢉྔͷ / iOSDC Japan 2017 / ©
Hisakuni Fujimoto, 2017 9
ܭࢉྔ҆ • ͬ͘͟ΓͱݟੵΔ͜ͱ͕ॏཁ • ਖ਼֬ʹΛܭࢉ͢ΔͷͰͳ͍ SwiftϓϩάϥϚͷͨΊͷࠓ͞Βฉ͚ͳ͍ܭࢉྔͷ / iOSDC Japan 2017
/ © Hisakuni Fujimoto, 2017 10
࣌ؒܭࢉྔͱۭؒܭࢉྔ • ࣌ؒܭࢉྔ (Time complexity) ʹ ඞཁͳ࣌ؒͷ҆ • ۭؒܭࢉྔ (Space
complexity) ʹ ඞཁͳϝϞϦʔۭؒͷ҆ ࣮ࡍʹ୯ʹʮܭࢉྔʯͱදݱ͞ΕΔ͜ͱ͕ଟ͍ɻ ͦͷ߹ʮ࣌ؒܭࢉྔʯͷ͜ͱΛࢦ͢ɻ SwiftϓϩάϥϚͷͨΊͷࠓ͞Βฉ͚ͳ͍ܭࢉྔͷ / iOSDC Japan 2017 / © Hisakuni Fujimoto, 2017 11
O(1) ܭࢉྔ ʹ ೖྗͷେ͖͞ʹؔͳ͘Ұఆ (ྫ) ྻͷ n ൪ͷཁૉͷΞΫηε SwiftϓϩάϥϚͷͨΊͷࠓ͞Βฉ͚ͳ͍ܭࢉྔͷ /
iOSDC Japan 2017 / © Hisakuni Fujimoto, 2017 12
O(n) ܭࢉྔ ʹ ೖྗͷେ͖͞ʹൺྫ (ྫ) ྻͷ࠷ॳ͔Β n ൪·Ͱͷશͯͷσʔλʹॱ൪ʹΞΫηε SwiftϓϩάϥϚͷͨΊͷࠓ͞Βฉ͚ͳ͍ܭࢉྔͷ /
iOSDC Japan 2017 / © Hisakuni Fujimoto, 2017 13
Α͋͘Δܭࢉྔ ܭࢉྔ (Oه๏) SwiftυΩϡϝϯτ O(1) ◦ O(n) ◦ O(log n)
× ೋ୳ࡧͳͲ O(n log n) × ϚʔδιʔτͳͲͷฒ ସ͑ΞϧΰϦζϜ SwiftϓϩάϥϚͷͨΊͷࠓ͞Βฉ͚ͳ͍ܭࢉྔͷ / iOSDC Japan 2017 / © Hisakuni Fujimoto, 2017 14
SwiftϓϩάϥϚͷͨΊͷࠓ͞Βฉ͚ͳ͍ܭࢉྔͷ / iOSDC Japan 2017 / © Hisakuni Fujimoto, 2017
15
Swift Standard Library ͱܭࢉྔ https://developer.apple.com/documentation/swift SwiftϓϩάϥϚͷͨΊͷࠓ͞Βฉ͚ͳ͍ܭࢉྔͷ / iOSDC Japan 2017
/ © Hisakuni Fujimoto, 2017 16
Swift Standard Library ͱܭࢉྔ ίϨΫγϣϯʹؔ͢ΔυΩϡϝϯτͷཁॴཁॴͰɺ ܭࢉྔʹ͍ͭͯͷهड़͕͋Δɻ ྻ(Array)ɺࣙॻ(Dictionary)ɺू߹(Set)ͳͲ SwiftϓϩάϥϚͷͨΊͷࠓ͞Βฉ͚ͳ͍ܭࢉྔͷ / iOSDC
Japan 2017 / © Hisakuni Fujimoto, 2017 17
Swift Standard Library ͱܭࢉྔ υΩϡϝϯτʹग़ݱ͢Δ༻ޠ O(1), O(n) O(m), O(n +
m) 2ͭͷίϨΫγϣϯ͕བྷΉܭࢉ Complexity ܭࢉྔ Amortized (complexity) ۉ͠(ܭࢉྔ) SwiftϓϩάϥϚͷͨΊͷࠓ͞Βฉ͚ͳ͍ܭࢉྔͷ / iOSDC Japan 2017 / © Hisakuni Fujimoto, 2017 18
Sequence ϓϩτίϧ • ͦΕͧΕͷཁૉʹ܁Γฦ͠ΞΫηεͰ͖ΔܕΛఏڙ • for i in γʔέϯε {
... } • filter, map, reduce ͳͲͷϝιουΛ࣮ SwiftϓϩάϥϚͷͨΊͷࠓ͞Βฉ͚ͳ͍ܭࢉྔͷ / iOSDC Japan 2017 / © Hisakuni Fujimoto, 2017 19
Sequence ϓϩτίϧ Expected Performance: A sequence should provide its iterator
in O(1). The Sequence protocol makes no other requirements about element access, so routines that traverse a sequence should be considered O(n) unless documented otherwise.1 • γʔέϯεɺཁૉͷ܁Γฦ͠Λ O(1) Ͱఏڙ͖͢ • ཁૉΛॱʹͨͲΔॲཧͷܭࢉྔ O(n) 1 https://developer.apple.com/documentation/swift/sequence SwiftϓϩάϥϚͷͨΊͷࠓ͞Βฉ͚ͳ͍ܭࢉྔͷ / iOSDC Japan 2017 / © Hisakuni Fujimoto, 2017 20
countϓϩύςΟ Complexity: O(1) if the collection conforms to RandomAccessCollection; otherwise,
O(n), where n is the length of the collection.2 ʮίϨΫγϣϯ͕ RandomAccessCollection ϓϩτίϧʹ४ڌ͠ ͍ͯΔ߹ O(1)ɺͦͷଞͷ߹ O(n)ʯ 2 https://developer.apple.com/documentation/swift/collection/2885508-count SwiftϓϩάϥϚͷͨΊͷࠓ͞Βฉ͚ͳ͍ܭࢉྔͷ / iOSDC Japan 2017 / © Hisakuni Fujimoto, 2017 21
countϓϩύςΟ Discussion: To check whether a collection is empty, use
its isEmpty property instead of comparing count to zero. Unless the collection guarantees random-access performance, calculating count can be an O(n) operation.2 ʮίϨΫγϣϯ͕ۭ͔Ͳ͏͔ௐΔͨΊʹɺcount Λθϩͱൺ ֱ͢ΔͷͰͳ͘ɺisEmpty ϓϩύςΟΛ͍ͳ͍͞ʯ 2 https://developer.apple.com/documentation/swift/collection/2885508-count SwiftϓϩάϥϚͷͨΊͷࠓ͞Βฉ͚ͳ͍ܭࢉྔͷ / iOSDC Japan 2017 / © Hisakuni Fujimoto, 2017 22
isEmptyϓϩύςΟ Complexity: O(1)3 3 https://developer.apple.com/documentation/swift/collection/2886121-isempty SwiftϓϩάϥϚͷͨΊͷࠓ͞Βฉ͚ͳ͍ܭࢉྔͷ / iOSDC Japan 2017
/ © Hisakuni Fujimoto, 2017 23
isEmptyϓϩύςΟ Discussion: When you need to check whether your collection
is empty, use the isEmpty property instead of checking that the count property is equal to zero. ... 3 ʮίϨΫγϣϯ͕ۭ͔Ͳ͏͔ௐΔͨΊʹɺcount Λθϩͱൺ ֱ͢ΔͷͰͳ͘ɺisEmpty ϓϩύςΟΛ͍ͳ͍͞ʯ 3 https://developer.apple.com/documentation/swift/collection/2886121-isempty SwiftϓϩάϥϚͷͨΊͷࠓ͞Βฉ͚ͳ͍ܭࢉྔͷ / iOSDC Japan 2017 / © Hisakuni Fujimoto, 2017 24
ۉ͠ܭࢉྔ (Amortized complexity) ۉ͠ܭࢉྔ = ܭࢉྔͷฏۉ ࠷ѱͷܭࢉྔ͚ͩΛݟΔͷ͕൵؍తʹա͗Δͱ͖ʹɺ ܭࢉྔͷฏۉΛݟΔ4 4 ʮಈతྻͷՃίετͳͥ
O(1)?ʯ: http://chasen.org/~taku/blog/archives/2007/02/_o1.html SwiftϓϩάϥϚͷͨΊͷࠓ͞Βฉ͚ͳ͍ܭࢉྔͷ / iOSDC Japan 2017 / © Hisakuni Fujimoto, 2017 25
appendϝιου Complexity: Amortized O(1) over many additions. ... 5 Swift
ͷArrayܕͷΑ͏ʹ ༰ྔΛഒʑ(ࢦత)ʹ૿͍ͯ͘͠ಈతྻͷ ʮۉ͠ܭࢉྔʯ O(1) 4 6 6 reserveCapacity: https://developer.apple.com/documentation/swift/array/1538966-reservecapacity 4 ʮಈతྻͷՃίετͳͥ O(1)?ʯ: http://chasen.org/~taku/blog/archives/2007/02/_o1.html 5 https://developer.apple.com/documentation/swift/array/1538872-append SwiftϓϩάϥϚͷͨΊͷࠓ͞Βฉ͚ͳ͍ܭࢉྔͷ / iOSDC Japan 2017 / © Hisakuni Fujimoto, 2017 26
·ͱΊ • ܭࢉྔ ʹ ΞϧΰϦζϜͷ࣮ߦʹඞཁͳ࣌ؒɾۭؒͷ҆ • O(1) ʹ ೖྗʹΑΒͣɺܭࢉྔ͕Ұఆ •
O(n) ʹ ೖྗʹൺྫͯ͠ɺܭࢉྔ͕૿Ճ • `Swift ͷυΩϡϝϯτͷܭࢉྔʹ͍ͭͯͷهड़ʹΛ௨ͦ͏ • ܭࢉྔʹؾΛͬͯɺ͘͘͞͞ಈ͘ΞϓϦΛ࡞Ζ͏ SwiftϓϩάϥϚͷͨΊͷࠓ͞Βฉ͚ͳ͍ܭࢉྔͷ / iOSDC Japan 2017 / © Hisakuni Fujimoto, 2017 27
·ͱΊ • ܭࢉྔ ʹ ΞϧΰϦζϜͷ࣮ߦʹඞཁͳ࣌ؒɾۭؒͷ҆ • O(1) ʹ ೖྗʹΑΒͣɺܭࢉྔ͕Ұఆ •
O(n) ʹ ೖྗʹൺྫͯ͠ɺܭࢉྔ͕૿Ճ • `Swift ͷυΩϡϝϯτͷܭࢉྔʹ͍ͭͯͷهड़ʹΛ௨ͦ͏ • ܭࢉྔʹؾΛͬͯɺ͘͘͞͞ಈ͘ΞϓϦΛ࡞Ζ͏ SwiftϓϩάϥϚͷͨΊͷࠓ͞Βฉ͚ͳ͍ܭࢉྔͷ / iOSDC Japan 2017 / © Hisakuni Fujimoto, 2017 28
͝੩ௌɺ͋Γ͕ͱ͏͍͟͝·ͨ͠ʂ !! iOSDC JAPAN 2017 ָ͠Έ·͠ΐ͏ !! εϥΠυͪ͜Β https://speakerdeck.com/fhisa/ swiftpuroguramafalsetamefalsejin-sarawen-kenaiji-suan-liang-
falsehua SwiftϓϩάϥϚͷͨΊͷࠓ͞Βฉ͚ͳ͍ܭࢉྔͷ / iOSDC Japan 2017 / © Hisakuni Fujimoto, 2017 29