Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Sign up for free
Menu
Search
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Pricing
Search
Sign in
Sign up for free
Swiftプログラマのための今さら聞けない計算量の話
Search
Hisakuni Fujimoto
September 16, 2017
Programming
3.3k
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
Everything will be SERVERLESS — 信じて運用した10年の経験値 / Everything Will be Serverless — Lessons Learned from 10 Years of Operational Experience
seike460
PRO
1
480
SONY CISC-NEWS NWS-1750 + NWB-225 フレームバッファの NetBSD/news68k ドライバ実装 / OSC2026Hiroshima
tsutsui
0
140
WebMCP Challenge に星空観察アプリで参加した話
okajun35
0
190
Snowflakeで業務アプリを作ろう。 Snowflakeのアプリ機能解説&実践ガイド
ayumu_yamaguchi
2
300
モバイル交通系ICへのチャージ実例から考える、クロスプラットフォーム開発におけるiOS実機テスト設計とCI運用
yusuga
1
540
Intent as Code
shoppingjaws
6
1.1k
UnityでSystem.Net.WebSocketsなWebSocketサーバが動かないのでUnity Monoのコードを覗いてみた / about implementing websocket server with unity mono
drumath2237
1
350
ハーネス設計入門 〜 基礎知識の整理から実務へのステップアップ 〜
kinopeee
17
15k
WebRTC映像をAirPlayに対応させる挑戦.pdf
monolithic_adam
0
300
Streamlitで実現する自然言語データアプリ開発
ayumu_yamaguchi
1
310
GitHubハンズオン講座 — 実務レベルのチーム開発のフローを身につけよう
junhat6
0
100
Augmenting AI with the Power of Jakarta EE
ivargrimstad
0
290
Featured
See All Featured
Money Talks: Using Revenue to Get Sh*t Done
nikkihalliwell
0
500
Facilitating Awesome Meetings
lara
57
7.1k
brightonSEO & MeasureFest 2025 - Christian Goodrich - Winning strategies for Black Friday CRO & PPC
cargoodrich
3
850
Future Trends and Review - Lecture 12 - Web Technologies (1019888BNR)
signer
PRO
0
3.8k
Breaking role norms: Why Content Design is so much more than writing copy - Taylor Woolridge
uxyall
1
420
The agentic SEO stack - context over prompts
schlessera
0
940
[SF Ruby Conf 2025] Rails X
palkan
3
1.4k
SEO for Brand Visibility & Recognition
aleyda
0
4.7k
The Cult of Friendly URLs
andyhume
79
7k
A brief & incomplete history of UX Design for the World Wide Web: 1989–2019
jct
2
510
WENDY [Excerpt]
tessaabrams
14
39k
Improving Core Web Vitals using Speculation Rules API
sergeychernyshev
21
1.6k
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