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
Laravel Collectionの計算量を調べてみた
Search
Ryo Tomidokoro
September 26, 2018
Programming
3
8.3k
Laravel Collectionの計算量を調べてみた
Laravel Collectionの各メソッドの計算量を確認してみた。
Ryo Tomidokoro
September 26, 2018
Tweet
Share
More Decks by Ryo Tomidokoro
See All by Ryo Tomidokoro
集中して作業する技術/how_to_work_deeply
hanhan1978
61
40k
PHPでデータベースを作ってみた/create-data-with-php
hanhan1978
10
9.1k
ADRを一年運用してみた/adr_after_a_year
hanhan1978
8
3.4k
B+木入門:PHPで理解する データベースインデックスの仕組み/b-plus-tree-101
hanhan1978
5
4.5k
ADRを一年運用してみた/our_story_about_adr
hanhan1978
5
2k
PHPで学ぶ Session の基本と応用 / web-app-session-101-2024
hanhan1978
12
5.5k
レガシー回避のPHP開発術/avoid_php_legacy
hanhan1978
16
12k
Laravel Collectionの計算量を調べてみた2023/laravel_collection_time_complexity_2023
hanhan1978
1
1.4k
PHP で学ぶ Cache の距離の話 / study_cache_with_php
hanhan1978
7
2.1k
Other Decks in Programming
See All in Programming
ActiveSupport::Notifications supporting instrumentation of Rails apps with OpenTelemetry
ymtdzzz
1
250
React への依存を最小にするフロントエンド設計
takonda
12
3.3k
React CompilerとFine Grained Reactivityと宣言的UIのこれから / The next chapter of declarative UI
ssssota
1
100
CSC509 Lecture 13
javiergs
PRO
0
110
OSSで起業してもうすぐ10年 / Open Source Conference 2024 Shimane
furukawayasuto
0
110
Arm移行タイムアタック
qnighy
0
340
cmp.Or に感動した
otakakot
3
230
A Journey of Contribution and Collaboration in Open Source
ivargrimstad
0
1k
Modular Monolith Monorepo ~シンプルさを保ちながらmonorepoのメリットを最大化する~
yuisakamoto
5
340
ヤプリ新卒SREの オンボーディング
masaki12
0
130
Creating a Free Video Ad Network on the Edge
mizoguchicoji
0
120
subpath importsで始めるモック生活
10tera
0
320
Featured
See All Featured
RailsConf 2023
tenderlove
29
900
Scaling GitHub
holman
458
140k
Building Better People: How to give real-time feedback that sticks.
wjessup
364
19k
Fireside Chat
paigeccino
34
3k
Save Time (by Creating Custom Rails Generators)
garrettdimon
PRO
27
840
Documentation Writing (for coders)
carmenintech
65
4.4k
Being A Developer After 40
akosma
87
590k
Six Lessons from altMBA
skipperchong
27
3.5k
Gamification - CAS2011
davidbonilla
80
5k
Large-scale JavaScript Application Architecture
addyosmani
510
110k
How to Think Like a Performance Engineer
csswizardry
20
1.1k
Teambox: Starting and Learning
jrom
133
8.8k
Transcript
Laravel CollectionͷܭࢉྔΛௐͯΈͨ 1)1ษڧձ *OOPWBUPS+BQBO*OD ॴ྄ &OHJOFFS
ܭࢉྔʹ͍͓ͭͯ͞Β͍ ຊ࣌ؒతܭࢉྔΛѻ͍·͢
ྫ͑ϨϏϡʔ͍ͯ͠Δ࣌
ʮ͜ͷॲཧͦ͏ʯ ͜Εͩͱ͔Γʹ͍͘ɻ ॲཧͷ࣌ؒతΛڞ௨ࣝͰ͍͑ͨ
ӳޠͩͱ Time Complexity ࣌ؒෳࡶੑ ϓϩάϥϜͷॲཧʹ ͲΕ͘Β͍͕͔͔࣌ؒΔ͔Λ ֶతʹѻ͏
Oه๏ O(1) O(log n) O(n) O(n * log n) O(n^2)
ϓϩάϥϜͷ࣌ؒతܭࢉྔΛද͢
Oه๏ σʔλྔ͕૿Ճͨ͠߹ͷ ॲཧ࣌ؒͷ૿Ճ͕͔Δ
[άϥϑҾ༻] ։ൃ৽ଔʹ๋͙ɺجຊͷΞϧΰϦζϜͱܭࢉྔ http://www.techscore.com/blog/2016/08/08/։ൃ৽ଔʹ๋͙ɺجຊͷΞϧΰϦζϜͱܭࢉྔ/ σʔλྔͱܭࢉྔ
ܭࢉྔͱΞϧΰϦζϜ ΞϧΰϦζϜ ܭࢉྔ όϒϧιʔτ 0 O? Ϛʔδιʔτ 0 OMPHO όΠφϦαʔν
0 MPHO ΞϧΰϦζϜʹΑͬͯܭࢉྔ͕ҟͳΔ
͞Βʹৄ͘͠Γ͍ͨਓ ֶΨʔϧ4 ཚΞϧΰϦζϜ 2ষͱ6ষΛಡΉ͠
ຊʹΔ
Laravel Collection֤ϝιουͷܭࢉྔ ͏ͪΐͬͱ͖Ε͍ʹ͠·͢…
ࡶʹ·ͱΊΔͱ • ΄ͱΜͲ O(n) • O(1) ͕9ݸ • O(n^2) Ҏ্͕8ݸ
ཁҙϝιου • 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)
࣮ଌͯ͠Έͨ
where - O(n)
ॲཧ࣌ؒ(ඵ) σʔλ݅
count - O(1)
ॲཧ࣌ؒ(ඵ) σʔλ݅
collapse - O(n^2)
ॲཧ࣌ؒ(ඵ) σʔλ݅
crossJoin - O(n^3)
ॲཧ࣌ؒ(ඵ) σʔλ݅
diff - O(n^t)
ॲཧ࣌ؒ(ඵ) σʔλ݅ tfieldʹґଘ͢Δ
ܭࢉྔ͕͔ͬͨͱͯ͠ Կ͔ྑ͍͜ͱ͋Δͷ͔ʁ
Βͳ͍ͱѱ͍͜ͱ͕ى͖Δ
࣮ࡍʹ͔͋ͬͨ͠Εͳ͍ ܭࢉྔ͕ʹͳͬͨίʔυྫ ※࣮Λݩʹͨ͠ϑΟΫγϣϯͰ͢
શ݅औಘ ϖʔδϯάͷͨΊʹ શ݅ϧʔϓͰճ͢ ྫ1
શ݅औಘ ϖʔδϯάͷͨΊʹ શ݅ϧʔϓͰճ͢ ϖʔδͷޙʹߦ͚ߦ͘΄Ͳ ϧʔϓ͕ճͬͯ͘ͳΔ O(n) ྫ1
߹Θٕͤ O(n^2) O(n)ΛೖΕࢠʹ͢ΕύϫʔΞοϓ ྫ2 ୈ1ϧʔϓͰશ݅ճ͢ O(n) ୈ2ϧʔϓશ݅ճ͢ O(n)
ୈҰҾ࠷େͰඦ݅ఔ͕ͩͬͨ ୈೋҾͷσʔλ͕͍ͯͬͯ͠… ྫ2 ୈ1ϧʔϓͰશ݅ճ͢ O(n) ୈ2ϧʔϓશ݅ճ͢ O(n)
ྫ2ͷΞϧΰϦζϜΛ࡞ͬͯ ࣮ଌͯ͠ΈΔ
σʔλྔʹΑΔੑೳͷྼԽ
ॲཧ࣌ؒ(ඵ) σʔλ݅ (ୈ1,2 Ҿಉ͡)
ͪΌΜͱO(n^2)ͷ άϥϑʹͳͬͨ
ࣄલʹݕͰ͖ͳ͍͔ʁ
࣮ྫ̍ɾ̎ͷίʔυ ୯ମςετ -> ௨ա ड͚ೖΕςετ -> ௨ա ௨աͯ͠͠·͍ͬͯͨ…
ෛՙςετ ίʔυϨϏϡʔ ࣄલݕग़Մೳͳࡆ
ෛՙςετ σʔλྔ͕ലେʹͳΔ͜ͱ͕ Θ͔͍ͬͯΔϓϩμΫτߦ͍ͬͯΔɻ ௨ৗͷϓϩμΫτͩͱ ͋Μ·ΓͬͯΔͷݟͨ͜ͱແ͍ɻ
ίʔυϨϏϡʔ ϨϏϡΞʔͷεΩϧܦݧʹґଘ ࣄલʹܭࢉྔʹ͍ͭͯ νʔϜͰษڧձͱ͔ͯ͠Ε ࢦఠˍमਖ਼؆୯ͩͱࢥ͏
ࢹπʔϧͰɺॲཧ࣌ؒͷϝτϦΫεΛݟͯ ཧ٧ΊͰॲཧ࣌ؒͷ͍෦ΛಛఆͰ͖Ε ·͋ɺٴୈͩͱࢥ͏ɻ ࠷ѱݟಀͯ͠
͓·͚
ܭࢉྔ͕Ұྎવ
σʔλͷू·ΓΛѻ͏ϓϩάϥϜ ܭࢉྔ͕໌ࣔ͞Ε͍ͯΔͱ͋Γ͕͍ͨ
͓͠·͍