Laravel Collectionの計算量を調べてみた

F04982ad61107b5408ad139966596316?s=47 Ryo Tomidokoro
September 26, 2018

Laravel Collectionの計算量を調べてみた

Laravel Collectionの各メソッドの計算量を確認してみた。

F04982ad61107b5408ad139966596316?s=128

Ryo Tomidokoro

September 26, 2018
Tweet

Transcript

  1. Laravel CollectionͷܭࢉྔΛௐ΂ͯΈͨ 1)1ษڧձ *OOPWBUPS+BQBO*OD ෋ॴ྄ &OHJOFFS

  2. ܭࢉྔʹ͍͓ͭͯ͞Β͍ ຊ೔͸࣌ؒతܭࢉྔΛѻ͍·͢

  3. ྫ͑͹ϨϏϡʔ͍ͯ͠Δ࣌

  4. ʮ͜ͷॲཧ஗ͦ͏ʯ ͜Εͩͱ෼͔Γʹ͍͘ɻ ॲཧͷ࣌ؒత଎౓Λڞ௨஌ࣝͰ఻͍͑ͨ

  5. ӳޠͩͱ Time Complexity ࣌ؒෳࡶੑ ϓϩάϥϜͷॲཧʹ ͲΕ͘Β͍͕͔͔࣌ؒΔ͔Λ ਺ֶతʹѻ͏

  6. Oه๏ O(1) O(log n) O(n) O(n * log n) O(n^2)

    ϓϩάϥϜͷ࣌ؒతܭࢉྔΛද͢
  7. Oه๏ σʔλྔ͕૿Ճͨ͠৔߹ͷ ॲཧ࣌ؒͷ૿Ճ܏޲͕෼͔Δ

  8. [άϥϑҾ༻] ։ൃ৽ଔʹ๋͙ɺجຊͷΞϧΰϦζϜͱܭࢉྔ http://www.techscore.com/blog/2016/08/08/։ൃ৽ଔʹ๋͙ɺجຊͷΞϧΰϦζϜͱܭࢉྔ/ σʔλྔͱܭࢉྔ

  9. ܭࢉྔͱΞϧΰϦζϜ ΞϧΰϦζϜ ܭࢉྔ όϒϧιʔτ 0 O? Ϛʔδιʔτ 0 OMPHO όΠφϦαʔν

    0 MPHO ΞϧΰϦζϜʹΑͬͯܭࢉྔ͕ҟͳΔ
  10. ͞Βʹৄ͘͠஌Γ͍ͨਓ ਺ֶΨʔϧ4 ཚ୒ΞϧΰϦζϜ 2ষͱ6ষΛಡΉ΂͠

  11. ຊ୊ʹ໭Δ

  12. Laravel Collection֤ϝιουͷܭࢉྔ ΋͏ͪΐͬͱ͖Ε͍ʹ͠·͢…

  13. ࡶʹ·ͱΊΔͱ • ΄ͱΜͲ O(n) • O(1) ͕9ݸ • O(n^2) Ҏ্͕8ݸ

  14. ཁ஫ҙϝιου • collapse O(n^2) • crossJoin O(n^3) • diff O(n^t)

    • diffAssoc O(n^t) • flatMap O(n^2) • flatten O(n^2) • groupBy O(n^2) • intersect O(n^2)
  15. ࣮ଌͯ͠Έͨ

  16. where - O(n)       

       ॲཧ࣌ؒ(ඵ) σʔλ݅਺
  17. count - O(1)       

       ॲཧ࣌ؒ(ඵ) σʔλ݅਺
  18. collapse - O(n^2)       

        ॲཧ࣌ؒ(ඵ) σʔλ݅਺
  19. crossJoin - O(n^3)       

       ॲཧ࣌ؒ(ඵ) σʔλ݅਺
  20. diff - O(n^t)       

       ॲཧ࣌ؒ(ඵ) σʔλ݅਺ t͸field਺ʹґଘ͢Δ
  21. ܭࢉྔ͕෼͔ͬͨͱͯ͠ Կ͔ྑ͍͜ͱ͋Δͷ͔ʁ

  22. ஌Βͳ͍ͱѱ͍͜ͱ͕ى͖Δ

  23. ࣮ࡍʹ͔͋ͬͨ΋͠Εͳ͍ ܭࢉྔ͕໰୊ʹͳͬͨίʔυྫ ※࣮࿩Λݩʹͨ͠ϑΟΫγϣϯͰ͢

  24. શ݅औಘ ϖʔδϯάͷͨΊʹ શ݅ϧʔϓͰճ͢ ྫ1

  25. શ݅औಘ ϖʔδϯάͷͨΊʹ શ݅ϧʔϓͰճ͢ ϖʔδͷޙ൒ʹߦ͚͹ߦ͘΄Ͳ ϧʔϓ͕ճͬͯ஗͘ͳΔ O(n) ྫ1

  26. ߹Θٕͤ O(n^2) O(n)ΛೖΕࢠʹ͢Ε͹ύϫʔΞοϓ ྫ2 ୈ1ϧʔϓͰશ݅ճ͢ O(n) ୈ2ϧʔϓ΋શ݅ճ͢ O(n)

  27. ୈҰҾ਺͸࠷େͰ਺ඦ݅ఔ౓͕ͩͬͨ ୈೋҾ਺ͷσʔλ਺͕੒௕͍ͯͬͯ͠… ྫ2 ୈ1ϧʔϓͰશ݅ճ͢ O(n) ୈ2ϧʔϓ΋શ݅ճ͢ O(n)

  28. ྫ2ͷΞϧΰϦζϜΛ࡞ͬͯ ࣮ଌͯ͠ΈΔ

  29. σʔλྔʹΑΔੑೳͷྼԽ         

          ॲཧ࣌ؒ(ඵ) σʔλ݅਺ (ୈ1,2 Ҿ਺ಉ͡)
  30. ͪΌΜͱO(n^2)ͷ άϥϑʹͳͬͨ

  31. ࣄલʹݕ஌Ͱ͖ͳ͍͔ʁ

  32. ࣮͸ྫ̍ɾ̎ͷίʔυ͸ ୯ମςετ -> ௨ա ड͚ೖΕςετ -> ௨ա ௨աͯ͠͠·͍ͬͯͨ…

  33. ෛՙςετ ίʔυϨϏϡʔ ࣄલݕग़Մೳͳࡆ

  34. ෛՙςετ σʔλྔ͕ലେʹͳΔ͜ͱ͕ Θ͔͍ͬͯΔϓϩμΫτ͸ߦ͍ͬͯΔɻ ௨ৗͷϓϩμΫτͩͱ ͋Μ·Γ΍ͬͯΔͷݟͨ͜ͱແ͍ɻ

  35. ίʔυϨϏϡʔ ϨϏϡΞʔͷεΩϧ΍ܦݧʹґଘ ࣄલʹܭࢉྔʹ͍ͭͯ νʔϜ಺Ͱษڧձͱ͔ͯ͠Ε͹ ࢦఠˍमਖ਼͸؆୯ͩͱࢥ͏

  36. ؂ࢹπʔϧͰɺॲཧ࣌ؒͷϝτϦΫεΛݟͯ ཧ٧ΊͰॲཧ࣌ؒͷ஗͍෦෼ΛಛఆͰ͖Ε͹ ·͋ɺٴୈ఺ͩͱࢥ͏ɻ ࠷ѱݟಀͯ͠΋

  37. ͓·͚

  38. ܭࢉྔ͕Ұ໨ྎવ

  39. σʔλͷू·ΓΛѻ͏ϓϩάϥϜ͸ ܭࢉྔ͕໌ࣔ͞Ε͍ͯΔͱ͋Γ͕͍ͨ

  40. ͓͠·͍