Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Swiftプログラマのための今さら聞けない計算量の話

Sponsored · Ship Features Fearlessly Turn features on and off without deploys. Used by thousands of Ruby developers.

 Swiftプログラマのための今さら聞けない計算量の話

もっさりアプリをサクサクにするための基本の「キ」

Avatar for Hisakuni Fujimoto

Hisakuni Fujimoto

September 16, 2017
Tweet

Other Decks in Programming

Transcript

  1. ࣗݾ঺հ • ౻ຊঘ๜ (53ࡀɺ1964ੜ) • ϑϦʔϥϯεϓϩάϥϚ • fhisa (Twitter, GitHub

    ͳͲ) • RubyCocoa(2001-), SwiftyLayout(2015-) • Cocoaษڧձ (Bukuro.swiftɺؔ౦swiftษڧձ) SwiftϓϩάϥϚͷͨΊͷࠓ͞Βฉ͚ͳ͍ܭࢉྔͷ࿩ / iOSDC Japan 2017 / © Hisakuni Fujimoto, 2017 2
  2. ΞδΣϯμ • ܭࢉྔ௒ೖ໳ • Swift Standard Library ͱܭࢉྔ • ·ͱΊ

    SwiftϓϩάϥϚͷͨΊͷࠓ͞Βฉ͚ͳ͍ܭࢉྔͷ࿩ / iOSDC Japan 2017 / © Hisakuni Fujimoto, 2017 3
  3. ܭࢉྔ௒ೖ໳ 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
  4. ܭࢉྔ௒ೖ໳ 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
  5. ΞϧΰϦζϜͱ͸ʁ • ୔ࢁͷσʔλ͔ΒಛఆσʔλΛݟ͚ͭΔํ๏ (ೋ෼୳ࡧͳͲ) • ୔ࢁͷσʔλΛॱʹฒ΂Δํ๏ (ϚʔδιʔτͳͲ) • ࠷େެ໿਺ΛٻΊΔํ๏ (ϢʔΫϦουͷޓআ๏)

    • ύζϧΛղ͘ํ๏ (ϋϊΠͷౝͳͲ) SwiftϓϩάϥϚͷͨΊͷࠓ͞Βฉ͚ͳ͍ܭࢉྔͷ࿩ / iOSDC Japan 2017 / © Hisakuni Fujimoto, 2017 8
  6. ࣌ؒܭࢉྔͱۭؒܭࢉྔ • ࣌ؒܭࢉྔ (Time complexity) ʹ ඞཁͳ࣌ؒͷ໨҆ • ۭؒܭࢉྔ (Space

    complexity) ʹ ඞཁͳϝϞϦʔۭؒͷ໨҆ ࣮ࡍʹ͸୯ʹʮܭࢉྔʯͱදݱ͞ΕΔ͜ͱ͕ଟ͍ɻ ͦͷ৔߹ʮ࣌ؒܭࢉྔʯͷ͜ͱΛࢦ͢ɻ SwiftϓϩάϥϚͷͨΊͷࠓ͞Βฉ͚ͳ͍ܭࢉྔͷ࿩ / iOSDC Japan 2017 / © Hisakuni Fujimoto, 2017 11
  7. Α͋͘Δܭࢉྔ ܭࢉྔ (Oه๏) SwiftυΩϡϝϯτ O(1) ◦ O(n) ◦ O(log n)

    × ೋ෼୳ࡧͳͲ O(n log n) × ϚʔδιʔτͳͲͷฒ ΂ସ͑ΞϧΰϦζϜ SwiftϓϩάϥϚͷͨΊͷࠓ͞Βฉ͚ͳ͍ܭࢉྔͷ࿩ / iOSDC Japan 2017 / © Hisakuni Fujimoto, 2017 14
  8. Swift Standard Library ͱܭࢉྔ υΩϡϝϯτʹग़ݱ͢Δ༻ޠ O(1), O(n) O(m), O(n +

    m) 2ͭͷίϨΫγϣϯ͕བྷΉܭࢉ Complexity ܭࢉྔ Amortized (complexity) ۉ͠(ܭࢉྔ) SwiftϓϩάϥϚͷͨΊͷࠓ͞Βฉ͚ͳ͍ܭࢉྔͷ࿩ / iOSDC Japan 2017 / © Hisakuni Fujimoto, 2017 18
  9. Sequence ϓϩτίϧ • ͦΕͧΕͷཁૉʹ܁Γฦ͠ΞΫηεͰ͖ΔܕΛఏڙ • for i in γʔέϯε {

    ... } • filter, map, reduce ͳͲͷϝιουΛ࣮૷ SwiftϓϩάϥϚͷͨΊͷࠓ͞Βฉ͚ͳ͍ܭࢉྔͷ࿩ / iOSDC Japan 2017 / © Hisakuni Fujimoto, 2017 19
  10. 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
  11. 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
  12. 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
  13. 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
  14. ۉ͠ܭࢉྔ (Amortized complexity) ۉ͠ܭࢉྔ = ܭࢉྔͷฏۉ ࠷ѱͷܭࢉྔ͚ͩΛݟΔͷ͕൵؍తʹա͗Δͱ͖ʹɺ ܭࢉྔͷฏۉΛݟΔ4 4 ʮಈత഑ྻ΁ͷ௥Ճίετ͸ͳͥ

    O(1)?ʯ: http://chasen.org/~taku/blog/archives/2007/02/_o1.html SwiftϓϩάϥϚͷͨΊͷࠓ͞Βฉ͚ͳ͍ܭࢉྔͷ࿩ / iOSDC Japan 2017 / © Hisakuni Fujimoto, 2017 25
  15. 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
  16. ·ͱΊ • ܭࢉྔ ʹ ΞϧΰϦζϜͷ࣮ߦʹඞཁͳ࣌ؒɾۭؒͷ໨҆ • O(1) ʹ ೖྗʹΑΒͣɺܭࢉྔ͕Ұఆ •

    O(n) ʹ ೖྗʹൺྫͯ͠ɺܭࢉྔ͕૿Ճ • `Swift ͷυΩϡϝϯτͷܭࢉྔʹ͍ͭͯͷهड़ʹ໨Λ௨ͦ͏ • ܭࢉྔʹؾΛ࢖ͬͯɺ͘͘͞͞ಈ͘ΞϓϦΛ࡞Ζ͏ SwiftϓϩάϥϚͷͨΊͷࠓ͞Βฉ͚ͳ͍ܭࢉྔͷ࿩ / iOSDC Japan 2017 / © Hisakuni Fujimoto, 2017 27
  17. ·ͱΊ • ܭࢉྔ ʹ ΞϧΰϦζϜͷ࣮ߦʹඞཁͳ࣌ؒɾۭؒͷ໨҆ • O(1) ʹ ೖྗʹΑΒͣɺܭࢉྔ͕Ұఆ •

    O(n) ʹ ೖྗʹൺྫͯ͠ɺܭࢉྔ͕૿Ճ • `Swift ͷυΩϡϝϯτͷܭࢉྔʹ͍ͭͯͷهड़ʹ໨Λ௨ͦ͏ • ܭࢉྔʹؾΛ࢖ͬͯɺ͘͘͞͞ಈ͘ΞϓϦΛ࡞Ζ͏ SwiftϓϩάϥϚͷͨΊͷࠓ͞Βฉ͚ͳ͍ܭࢉྔͷ࿩ / iOSDC Japan 2017 / © Hisakuni Fujimoto, 2017 28