36Fermer38
robinHoodLe 26/04/2011 à 20:23
je suis tombé sur des pages benchmarquant foreach, array_walk, array_map, et toute indiquais que foreach étais imbattable

bref je me suis fait mon bench à moi,
source bench
),3); ?>[nosmile]<?php $data = array ( 0 => array ( 'nom' => 'Love Me Body', 'ref' => '8705body..', 'prix' => '31.00', 'txt' => 'Un body plein d\'érotisme en dentelle rouge...', 'img' => '2', ), 1 => array ( 'prix' => '31.00', 'img' => '2', 'ref' => '0303body..', 'txt' => 'Tout en charme ce body avec ses liserets fushia sur résille noire...', 'nom' => 'Heart Body', ), 2 => array ( 'prix' => '31.00', 'img' => '2', 'ref' => '8662body..', 'txt' => 'Captivant body noir, idéal pour une soirée coquine a deux. Le tour de la ceinture est agrémenté de zircons chatoyants, ainsi qu\'une broche sertie de pierres brillantes sur le buste.', 'nom' => 'Forget Me Not Body', ), 3 => array ( 'nom' => 'Combinaison bodystocking Lingerie', 'ref' => 'CR3237body..', 'prix' => '19.99', 'txt' => 'Plus qu\'un simple bodystocking, presqu\'une seconde peau !', 'img' => '2', ), 4 => array ( 'prix' => '27.99', 'img' => '2', 'ref' => 'CR3167body', 'txt' => 'Ensemble très sexy comprenant une guepière à manches longues avec un ruban permettant de cintrer la taille, incluant porte-jarretelles réglable et son string assorti. Couleur : Rose Tailles disponibles : SM ou LXL Matière : 85% Polyestere, 15% Elastane', 'nom' => 'Guepière filet Rose', ), 5 => array ( 'nom' => 'Combinaison bodystocking', 'ref' => '7010-BLKBODY', 'prix' => '19.99', 'txt' => 'Combinaison bodystocking noir...', 'img' => '2', ), 6 => array ( 'prix' => '40.00', 'img' => '2', 'ref' => 'CR3229body', 'txt' => 'Ensemble top et pantalon en dentelle transparente, ultra sexy, ultra glamour....', 'nom' => 'Bodystocking 2 pièces', ), 7 => array ( 'prix' => '15.99', 'img' => '2', 'ref' => 'CR3243body..', 'txt' => 'Combinaison bodystocking avec ouverture a l\'entre-jambe. Taille : S/M Livré en boite rigide.', 'nom' => 'Combinaison bodystocking', ), 8 => array ( 'prix' => '24.99', 'img' => '1', 'ref' => 'CR3093body', 'txt' => 'Body noir tres envoutant, a dentelle et volant sur le devant et resille au dos, avec bretelles rehausse de petits flots et fentes sur la poitrine et a l\'arriere.', 'nom' => 'Body dentelle ouvert', ), 9 => array ( 'nom' => 'clean', 'ref' => 'clean', 'prix' => '999', 'txt' => 'bla!', 'img' => '2', ) ); $keywords = array ( 0 => 'ref', 1 => 'nom', 2 => 'nom', 3 => 'img', 4 => 'desc', 5 => 'prix' ); $callback = function($w,$d,$n) { return 'w:'.$w.' n:'.$n; }; $design='<pre>truc machin chose</pre>'; $tstart = microtime(true); $iterate = 10000; while(--$iterate) { $arrayin = array_map(function($w){return '$'.$w.'$';},$keywords); $out=''; $n=0; reset($data); foreach($data as $dta) { $out .= str_replace($arrayin,array_map(function($w)use($callback,$dta,$n){return $callback($w,$dta,$n);},$keywords),$design); $n++; } } echo '<br> full array map benchmark : '.round(((microtime(true)-$tstart)*1000),3); $tstart = microtime(true); $iterate = 10000; while(--$iterate) { $out=''; $n=0; $arrayin = array(); array_walk($keywords,function($k)use($arrayin){$arrayin[]='$'.$k.'$';}); // faster reset($data); foreach($data as $dta) { $arrayout = array(); array_walk($keywords,function($w)use($arrayout,$callback,$dta,$n) { $arrayout[] = $callback($w,$dta,$n); //array_push($arrayout,$callback($w,$dta,$n)); }); $out .= str_replace($arrayin,$arrayout,$design); $n++; } } echo '<br> full array walk benchmark : '.round(((microtime(true)-$tstart)*1000),3); $tstart = microtime(true); $iterate = 10000; while(--$iterate) { $out=''; $n=0; $arrayin = array(); array_walk($keywords,function($k)use($arrayin){$arrayin[]='$'.$k.'$';}); // faster //foreach($keywords as $w) $arrayin[]='$'.$k.'$'; // slow! //foreach($keywords as $w) array_push($arrayin,'$'.$k.'$'); // slower reset($data); foreach($data as $dta) { $arrayout = array(); reset($keywords); foreach($keywords as $k) $arrayout[] = $callback($w,$dta,$n); $out .= str_replace($arrayin,$arrayout,$design); $n++; } } echo '<br> full foreach benchmark : '.round(((microtime(true)-$tstart)*1000),3); $tstart = microtime(true); $iterate = 10000; while(--$iterate) { $out=''; $n=0; $arrayin = array(); array_walk($keywords,function($k)use($arrayin){$arrayin[]='$'.$k.'$';}); // faster reset($data); foreach($data as $dta) { $out .= str_replace($arrayin,array_map(function($w)use($callback,$dta,$n){return $callback($w,$dta,$n);},$keywords),$design); $n++; } } echo '<br> best combinaison benchmark : '.round(((microtime(true)-$tstart)*1000),3); $tstart = microtime(true); $iterate = 10000; while(--$iterate) { $out=''; $n=0; $arrayin = array(); array_walk($keywords,function($k)use($arrayin){$arrayin[]='$'.$k.'$';}); // faster array_walk($data,function($dta)use($callback,$arrayin,$n,$keywords,$out,$design) { $out .= str_replace($arrayin,array_map(function($w)use($callback,$dta,$n){return $callback($w,$dta,$n);},$keywords),$design); $n++; }); } echo '<br> best combinaison benchmark with replacing the foreach with array walk : '.round(((microtime(true)-$tstart)*1000[/nosmile]


résultats :
full array map benchmark : 2914.525
 full array walk benchmark : 3107.557
 full foreach benchmark : 10894.946
 best combinaison benchmark : 2605.041
 best combinaison benchmark with replacing the foreach with array walk : 3053.353


conclusion : pas de conclusion c'est vraiment de la merde, et les bench que j'avais lu aussi ^^