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
symfony/aiとlaravel/boost
77web
0
120
AI がコードを書く時代における新卒エンジニアの仕事風景 (2026) / New Graduate Engineers in the Era of AI Coding (2026)
sushichan044
0
210
過去最大のMCPアップデート! 2026-07-28 RC版の謎に迫る
licux
6
460
PHPだって関数型したい 〜できること、できないこと〜 / fp-in-php
jsoizo
0
190
そのテスト、説明できますか?~LWテスト戦略FW~のご紹介
nakahara
0
200
気圧・高度・GPSを記録&可視化するアプリ「Koudo」を作った話
hjmkth
1
350
例外の正しい扱い方 そのエラー try-catchして大丈夫?
jinwatanabe
0
360
1年で人数1.5倍、PR数5.5倍増。 品質とアウトカムはどうなったか、 何が効いたか
ike002jp
0
120
トークンをケチるな、設計しろ:GitHub Copilotを賢く使うコンテキスト戦略
ochtum
0
310
霧の中の代数的エフェクト
funnyycat
1
340
【やさしく解説 設計編・中級 #4】ルールの寿命と、システムの年輪
panda728
PRO
2
110
【やさしく解説 設計編 #0】DDDのコード、読めるのに分からない人へ
panda728
PRO
2
250
Featured
See All Featured
[Rails World 2023 - Day 1 Closing Keynote] - The Magic of Rails
eileencodes
38
2.9k
GitHub's CSS Performance
jonrohan
1033
470k
Google's AI Overviews - The New Search
badams
0
1.1k
Lightning talk: Run Django tests with GitHub Actions
sabderemane
0
220
Bioeconomy Workshop: Dr. Julius Ecuru, Opportunities for a Bioeconomy in West Africa
akademiya2063
PRO
1
170
10 Git Anti Patterns You Should be Aware of
lemiorhan
PRO
659
62k
Abbi's Birthday
coloredviolet
3
8.6k
Efficient Content Optimization with Google Search Console & Apps Script
katarinadahlin
PRO
1
680
Crafting Experiences
bethany
1
210
Digital Ethics as a Driver of Design Innovation
axbom
PRO
1
340
Tips & Tricks on How to Get Your First Job In Tech
honzajavorek
1
560
Leadership Guide Workshop - DevTernity 2021
reverentgeek
1
320
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