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
Sponsored
·
Your Podcast. Everywhere. Effortlessly.
Share. Educate. Inspire. Entertain. You do you. We'll handle the rest.
→
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
Spring Security 実践 ─ GraphQL APIで実務に役立つ 認証・認可 を学ぶ
wagyu
0
230
Even G2とAWSで推しのエージェントを召喚しよう!
har1101
1
110
気圧・高度・GPSを記録&可視化するアプリ「Koudo」を作った話
hjmkth
1
260
メソッドのジェネリクスでGoの夢は広がるか? / Kyoto.go #65
utgwkk
3
770
Java × distroless で 軽量なコンテナイメージを / Java on Distroless
contour_gara
0
540
The NotImplementedError Problem in Ruby
koic
1
790
Vue × Nuxt × Oxc どこまで使える?実運用の現在地
andpad
0
250
Observability in Practice:Grafana 與 Edge Device SRE 的那些事
blueswen
0
160
PHPで使える日時の表現と、その知り方 #frontend_phpcon_do
o0h
PRO
0
240
3Dシーンの圧縮
fadis
1
770
その問い、本当に正しいですか?AI時代のエンジニアに必要な哲学と認知科学 / ai-philosophy-cognitive-science
minodriven
9
5.1k
TAKTでAI駆動開発の品質を設計する
j5ik2o
7
1.3k
Featured
See All Featured
GitHub's CSS Performance
jonrohan
1033
470k
Impact Scores and Hybrid Strategies: The future of link building
tamaranovitovic
0
310
How to audit for AI Accessibility on your Front & Back End
davetheseo
0
430
The Power of CSS Pseudo Elements
geoffreycrofte
82
6.3k
RailsConf & Balkan Ruby 2019: The Past, Present, and Future of Rails at GitHub
eileencodes
141
35k
Put a Button on it: Removing Barriers to Going Fast.
kastner
60
4.3k
Collaborative Software Design: How to facilitate domain modelling decisions
baasie
1
250
Tips & Tricks on How to Get Your First Job In Tech
honzajavorek
1
540
Color Theory Basics | Prateek | Gurzu
gurzu
0
360
Effective software design: The role of men in debugging patriarchy in IT @ Voxxed Days AMS
baasie
0
410
Kristin Tynski - Automating Marketing Tasks With AI
techseoconnect
PRO
0
270
Evolution of real-time – Irina Nazarova, EuRuKo, 2024
irinanazarova
9
1.4k
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