Abanga bahora bakandagira kuri PHP kubwimpamvu zitandukanye, ariko imwe mubitego byoroshye nukuntu code ishobora kuba mbi. Abantu bamwe ntibashobora kwemeranya ko isuku ya code hamwe n "" ubwiza ", ariko mugihe cyo kubungabunga codebase mugihe kirekire, urashobora gukiza ikipe yawe umwanya munini ufite code byoroshye gusoma.
Nkiri muri Apple, nateje imbere ecosystem yimikoreshereze yimari ikoreshwa nabafatanyabikorwa imbere. Mu myaka yashize, nahoraga nurira abaterankunga bashya mubikoresho byo gufasha muri rusange kubungabunga cyangwa kuzamura. Kimwe mubintu byingenzi byafashwe kuva icyo gihe nuburyo ari ngombwa gushobora gusoma code yawe nururimi karemano.
Birashimishije kubona ko code nyinshi zanditswe muburyo ugomba gusoma ukoresheje igice kinini cyacyo, hanyuma ugasubira muntangiriro kugirango wumve neza ibibera. Gukoresha imvugo yerekana cyane cyangwa APIs mugihe wandika code birashobora kugabanya cyane kubutaka no kuzamuka ibihe byabateza imbere.
Aha niho Ibyegeranyo bimurika. Ibyegeranyo bya Laravel bifata inzira igoye kandi byoroshye kubisoma no kubyumva. Kode yawe ihinduka urunigi rw'amategeko asoma nka "Shungura iyi array kumagambo gusa utangirira kuri a, hanyuma ushushanye igiciro cyumurongo, hanyuma ugabanye ibi kugeza kumafaranga yose." Biroroshye cyane gusobanura akamaro ko kwandika code yawe hamwe nururimi rwerekana, ariko iyo umaze kubigira akamenyero, biragoye kwiyumvisha uburyo ikindi kintu cyose cyemewe.
Birumvikana ko gusoma bidashobora kuza bitwaye imikorere bityo rero ni ngombwa kwemeza ko duhora duhitamo neza kandi dushyira imbere ibyasomwe aho bikwiye. Tugomba guhora twumva ibibera inyuma kandi tukamenya neza ibyo gucuruza duhura nabyo nukuntu twegera ibisubizo byacu. Ntabwo aribyiza ko code yacu isomwa niba ifata isegonda yuzuye kurenza igisubizo kidasomeka.
Reka twibire kandi turebe byimbitse kureba kuri PHP array hamwe nicyegeranyo hanyuma tugereranye imikorere hagati yabyo.
Imwe mu nyungu nini zo gukoresha ibyegeranyo bya Laravel ni syntax yabo neza, itanga uburyo bwo kuboha. Ibi bisubizo mubisuku kandi bisomeka kode, nkuko ushobora gukora ibikorwa byinshi murukurikirane udakeneye impinduka zigihe gito cyangwa ibikorwa byahamagaye byimikorere.
Reka tugereranye bimwe mubisanzwe bikoreshwa muburyo bukoreshwa muburyo bukurikira kugirango turebe uko PHP igereranya nicyegeranyo murugero rworoshye cyane.
PHP
array_filter($data, function($row) { return substr($row, 0, 1) === "a"; });
Ibyegeranyo
$data->filter(function($row) { return substr($row, 0, 1) === "a"; });
PHP
array_search(function($row) { return substr($row, 0, 1) === "a"; }, $data);
Ibyegeranyo
$data->search(function($row) { return substr($row, 0, 1) === "a"; });
PHP
array_map(function($row) { return "test"; }, $data);
Ibyegeranyo
$data->map(function($row) { return "test"; });
PHP
sort($data);
Ibyegeranyo
$data->sort();
PHP
foreach($loop as $item) { $doSomething = true; }
Ibyegeranyo
$data->each(function($row) { return "test"; });
PHP
array_reduce($data, function($carry, $row) { return $carry + strlen($row); });
Ibyegeranyo
$data->reduce(function($carry, $row) { return $carry + strlen($row); });
PHP
array_splice($data, count($data)/2);
Ibyegeranyo
$data->splice(count($data)/2);
Byose hamwe (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); });
Byose hamwe (Ibyegeranyo)
$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); });
Kugereranya
Hamwe nuburyo bworoshye, ntabwo bisa nkaho habaho gucuruza cyane mubisomwa kuri buri gikorwa cyumuntu ku giti cye, nubwo iyo urebye urugero aho ukeneye byose kugirango bikoreshwe kumurongo umwe, urashobora kubona neza ko ari byinshi mu magambo ahinnye kandi yoroshye gusoma mugihe ukoresheje iminyururu-uburyo mu cyegeranyo.
Aho guhora tugomba kwandika hejuru ya variable yawe hanyuma tugashyiraho impinduka nshya kubisohoka kumpera, turashobora guhuza gusa itegeko ryose kugeza tugeze kubyo twifuza. Ibyegeranyo biroroshye rwose gusoma bigoye cyane code yawe iba.
Nafashe ingero zavuzwe haruguru hanyuma ntanga amakuru yikizamini kugirango ngerageze kumenya niba ibyegeranyo byari byinshi cyangwa bike byakozwe kuruta imikorere isanzwe ya PHP.
Buri array yari ifite imirongo 100.000 idasanzwe nkibintu, kandi nayoboye buri gikorwa inshuro 100. Mugusoza, twabaze impuzandengo y'ibihe byose byo gusubiza.
Ibisubizo byanyuma biri hepfo:
========== 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 =======================
Nkuko ushobora kubibona neza, nubwo twunguka byinshi mubisomwa mubikusanyirizo, tubura umubare munini wimikorere mubice bimwe byingenzi.
Akayunguruzo , Ikarita , Foreach , na Kugabanya byose byihuse hamwe nibikorwa bisanzwe bya PHP. Foreach na Kugabanya mubyukuri itandukaniro ridasanzwe. Shakisha , Gutondeka , na Splice byose byerekana Ibyegeranyo nkuwatsinze, kandi Sort mubyukuri ni igihe kinini.
Ni ngombwa kandi kumenya ko ugomba gukora buri cyegeranyo uhereye kumurongo wubatswe wongeyeho akantu gato ko hejuru kugirango ushireho icyegeranyo kugirango utangire, ariko hamwe nubunini buke bwakazi hamwe nigihe, ibisubizo ni Birasobanutse neza.
Mubitekerezo byanjye (kandi ni igitekerezo gishingiye gusa kubisubizo), niba imikorere ari impungenge nini, nakomezanya nibikorwa bisanzwe bya PHP kumasoko ya Foreach byanze bikunze kandi birashoboka ko ari kimwe kugabanya ibyo ukeneye byose. Niba ukeneye gukora uburyo ubwo aribwo bwose kuri datasets nini, biragaragara ko Ibyegeranyo aribwo buryo bwiza bwo kugenda. Ibisigaye biregeranye cyane kuburyo byunvikana nkibyifuzo byawe bwite.
Icyo gihe, navuga ko gukusanya buri gihe byoroshye gusoma no kubungabunga.
Biragaragara, ugomba gufata aya makuru hanyuma ugafata icyemezo cyawe bwite, icyakora, niba umeze nkanjye, ndatekereza ko uzisanga unyerera mu byegeranyo byinshi muribi bikorwa hejuru. Ariko ndatekereza ko nzahamagara inyuma nkoresha →each
na →reduce
aho bikwiye kujya imbere!