Nacabku had iyo jeer waxay ku tuntaan PHP sababo kala duwan, laakiin mid ka mid ah bartilmaameedyada ugu fudud ayaa ah sida foosha xun ee koodku u noqon karo. Dadka qaarkiis waxaa laga yaabaa in aysan ku heshiin in nadaafadda koodka iyo "quruxda" ay tahay arrin, laakiin marka ay timaado ilaalinta codebases muddada dheer, waxaad badbaadin kartaa kooxdaada waqti badan adiga oo haysta kood si sahlan loo akhriyo.
Intii aan joogay Apple, waxaan sameeyay nidaamka deegaanka ee codsiyada maaliyadeed ee ay adeegsadaan daneeyayaasha gudaha. Sanadihii la soo dhaafay, waxaan si joogto ah u fuuli jiray horumariyeyaal cusub qalabka si aan uga caawiyo dayactirka guud ama hagaajinta. Mid ka mid ah waxyaabaha ugu waaweyn ee laga qaatay wakhtigaas ayaa ah sida ay muhiimka u tahay in lagu akhriyo koodka luqadda dabiiciga ah.
Waxaa xiiso leh in la ogaado in koodka intiisa badan uu u qoran yahay si aad u akhrido qayb muhiim ah oo ka mid ah, ka dibna dib ugu noqo bilawga si aad si buuxda u fahamto waxa socda. Isticmaalka luqado badan oo qeexan ama API-yada marka la qorayo koodka waxay si weyn u yareeyn kartaa wakhtiga fuulista iyo kor u qaadida horumarinta cusub.
Halkani waa halka ururintu ay ka iftiimaan. Laravel Collections waxay qaataan qaar ka mid ah safarro hannaan adag waxayna ka dhigaan si cajiib ah u fududahay in la akhriyo lana fahmo. Koodhkaagu wuxuu noqdaa silsilad amarro ah oo loo akhriyo sida "Ka shaandhayn shaxtan oo kaliya kelmado ka bilaabma a, ka dibna khariidad qaddarka kharashka safka, oo ugu dambayn tan ka dhig wadarta dhammaan kharashyada." Aad bay u fududahay inaad hoos u dhigto muhiimada ay leedahay in code-kaaga lagu qoro luqad aad u qeexan, laakiin marka aad caado ka dhigato, way adagtahay in la qiyaaso sida wax kale loo aqbali karo.
Dabcan, akhrintu kuma iman karto kharashka waxqabadka marka waa muhiim in la hubiyo in aan mar walba samaynayno doorashooyin sax ah oo aan mudnaanta siinayno oo keliya akhrinta halka ay ku habboon tahay. Waa inaan mar walba fahamnaa waxa ka dhacaya daaha gadaashiisa oo aan si buuxda uga war hayno ganacsiyada aan la kulanno habka aan u wajahayno xalalkayaga. Waxba uma fiicna in koodkayaga la akhriyi karo haddii ay qaadato ilbiriqsi buuxa oo ka dheer xal yar oo la akhriyi karo.
Aynu dhex muquuno oo aynu si qotodheer u eegno PHP arrays vs. Collections oo aynu barbar dhigno waxqabadka u dhexeeya labada.
Mid ka mid ah faa'iidooyinka ugu weyn ee isticmaalka Laravel Collections waa eray-bixintooda faseexa ah, taas oo u oggolaanaysa hababka silsiladda. Tani waxay keenaysaa kood nadiif ah oo la akhriyi karo, maadaama aad samayn karto hawlo badan oo isku xiga adoon u baahnayn doorsoomayaal ku meel gaar ah ama wicitaano shaqo oo adag oo buul leh.
Aynu is barbar dhigno qaar ka mid ah hawlaha wax-is-daba-marinta ee sida caadiga ah loo isticmaalo hoos si aan u aragno sida PHP uu u barbar dhigo Ururinta tusaale aad u fudud.
PHP
array_filter($data, function($row) { return substr($row, 0, 1) === "a"; });
Ururinta
$data->filter(function($row) { return substr($row, 0, 1) === "a"; });
PHP
array_search(function($row) { return substr($row, 0, 1) === "a"; }, $data);
Ururinta
$data->search(function($row) { return substr($row, 0, 1) === "a"; });
PHP
array_map(function($row) { return "test"; }, $data);
Ururinta
$data->map(function($row) { return "test"; });
PHP
sort($data);
Ururinta
$data->sort();
PHP
foreach($loop as $item) { $doSomething = true; }
Ururinta
$data->each(function($row) { return "test"; });
PHP
array_reduce($data, function($carry, $row) { return $carry + strlen($row); });
Ururinta
$data->reduce(function($carry, $row) { return $carry + strlen($row); });
PHP
array_splice($data, count($data)/2);
Ururinta
$data->splice(count($data)/2);
Dhammaantood (PHP)
$data = array_filter($data, function($row) { return substr($row, 0, 1) === "a"; }); $data = array_search(function($row) { return substr($row, 0, 1) === "a"; }, $data); $data = array_map(function($row) { return "test"; }, $data); sort($data); foreach($loop as $item) { $doSomething = true; } $sum = array_reduce($data, function($carry, $row) { return $carry + strlen($row); });
Dhammaantood (Uruurinta)
$sum = $data->filter(function($row) { return substr($row, 0, 1) === "a"; })->search(function($row) { return substr($row, 0, 1) === "a"; })->map(function($row) { return "test"; })->sort() ->each(function($row) { return "test"; })->reduce(function($carry, $row) { return $carry + strlen($row); });
Isbarbardhigga
Habkan fudud, uma muuqato inuu jiro wax-soo-saar weyn oo ku saabsan akhrinta hawl kasta oo shakhsi ah, inkasta oo markaad tixgeliso tusaalaha meesha aad u baahan tahay dhammaantood in lagu dabaqo hal qalab, waxaad si cad u arki kartaa inay ka badan tahay. si kooban oo sahlan in loo akhriyo marka la isticmaalayo hababka silsiladda ah ee ururinta.
Halkii aad si joogta ah u qori lahayd doorsoomahaaga ka dibna aad dejin lahayd doorsoome cusub oo wax soo saar ah dhamaadka, waxaan si fudud u xidhi karnaa amar kasta ilaa aan ka gaarno wax soo saarkayaga. Ururinta ayaa hubaal ah inay fududahay in la akhriyo mar kasta oo koodkaagu noqdo mid kakan.
Waxaan soo qaatay tusaalooyinka kor ku xusan oo aan soo saaray xoogaa xog tijaabo ah si aan isugu dayo in aan go'aamiyo in ururinta ay ka badan tahay ama ka yar tahay waxqabadka caadiga ah ee PHP.
Array kastaa wuxuu lahaa 100,000 oo xadhig random sida shay, waxaanan ku orday hawl kasta 100 jeer. Dhammaadkii, waxaanu xisaabinay celceliska dhammaan wakhtiyada jawaabta.
Natiijooyinka ugu dambeeya waa kuwan:
========== Tests Complete (ms) ========== php filter: 5.07 collect filter: 6.49 ======================= php search: 0.79 collect search: 0 ======================= php map: 3.45 collect map: 4.18 ======================= php sort: 25.27 collect sort: 11.18 ======================= php each: 1.03 collect each: 6.96 ======================= php reduce: 2.78 collect reduce: 7.75 ======================= php splice: 1 collect splice: 0.74 =======================
Sida aad si cad u arki karto, in kasta oo aanu ka helno wax akhris badan oo ka mid ah Ururinta, waxa aanu luminay wax qabad la taaban karo meelaha muhiimka ah qaarkood.
Shaandheeye , Khariidad , Foreach , iyo dhimis dhamaantood aad ayey ugu dheereeyaan shaqooyinka caadiga ah ee PHP. Foreach and Reduce run ahaantii waa kala duwanaansho la yaab leh oo weyn. Baadi , kala sooc , oo kala qaybi dhammaan waxay muujinayaan Ururinta sidii guulaystaha, oo Kala-soocida runtii waa waqti badbaadiye weyn.
Waxa kale oo muhiim ah in la ogaado in ay tahay in aad abuurto ururin kasta oo ka mid ah qalab la dhisay kaas oo ku daraya wax yar oo ka sarreeya si loo dejiyo ururinta si loo bilaabo, laakiin xitaa qadarka yar ee shaqada iyo wakhtiga dheeraadka ah, natiijadu waa aad u cad.
Fikradayda (oo waa ra'yi ku salaysan natiijooyinkan), haddii wax qabadku uu yahay walaac weyn, waxaan ku dhejin lahaa shaqeynta caadiga ah ee PHP ee loops Foreach hubaal iyo waxay u badan tahay inay la mid tahay baahi kasta oo la yareeyo . Haddii aad u baahan tahay inaad wax kala soociso kaydka xogta waaweyn, si cad Ururintu waa habka saxda ah ee loo maro. Inta soo hartay aad bay ugu dhow yihiin oo runtii waxay dareemayaan doorbid shakhsiyeed.
Halkaa marka ay marayso, waxaan odhan lahaa ururinta had iyo jeer way fududahay in la akhriyo lana ilaaliyo.
Sida iska cad, waa in aad qaadato macluumaadkan oo aad samayso go'aankaaga xog ogaal ah, si kastaba ha ahaatee, haddii aad tahay aniga oo kale, waxaan u malaynayaa in aad heli doonto adiga oo simbiriirixaya ururinta qaar badan oo ka mid ah hawlahan kore. Laakin waxaan filayaa in aan dib u wici doono isticmaalkeyga →each
iyo →reduce
halka ku haboon horay u socoshada!