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
·
SiteGround - Reliable hosting with speed, security, and support you can count on.
→
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
Webフレームワークの ベンチマークについて
yusukebe
0
170
Skillsは効率化、Agentsは"自分の拡張"——Builder時代のエージェント編成(CC Night 2026)
wemra
1
130
AIとASP.NET Coreで雑Webアプリを作った話
mayuki
0
630
JavaDoc 再入門
nagise
1
350
Semantic Version 単位で戦略を柔軟に変えて、パッケージアップデートを自動化する
daitasu
1
240
Lessons from Spec-Driven Development
simas
PRO
0
200
さぁV100、メモリをお食べ・・・
nilpe
0
140
DynamoDBには集計系のクエリがないけどなんとかしたい
musan
1
140
Make SRE Operations Easier with Azure SRE Agent
kkamegawa
0
6.1k
脅威をエンジニアリングの糧にして――現場編 / Turning Threats into Engineering Fuel — Field Edition
nrslib
0
280
OSもどきOS
arkw
0
560
技術記事、 専門家としてのプログラマ、 言語化
mizchi
13
6k
Featured
See All Featured
Designing for humans not robots
tammielis
254
26k
The Hidden Cost of Media on the Web [PixelPalooza 2025]
tammyeverts
2
330
Why Your Marketing Sucks and What You Can Do About It - Sophie Logan
marketingsoph
0
170
[RailsConf 2023] Rails as a piece of cake
palkan
59
6.7k
Beyond borders and beyond the search box: How to win the global "messy middle" with AI-driven SEO
davidcarrasco
3
160
Side Projects
sachag
455
43k
A Modern Web Designer's Workflow
chriscoyier
698
190k
The Invisible Side of Design
smashingmag
302
52k
Navigating the moral maze — ethical principles for Al-driven product design
skipperchong
2
390
Highjacked: Video Game Concept Design
rkendrick25
PRO
1
390
Faster Mobile Websites
deanohume
310
31k
Facilitating Awesome Meetings
lara
57
7k
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