array_* 㨠foreachã®å¦çæéæ¤è¨¼
ãªã«ãã§ã¡ããã£ã¨æ°ã«ãªã£ãã®ã§ãã£ããæ¤è¨¼ããã®ã§ã軽ãlogæ®ãã
åºæ¬çã«ãforeachã«æå©ãªå¦ç*1ããªã®ã§ãã¾ãåèç¨åº¦ã«è¦ã¦ããã ããã°ã
ãã è¨ãæ¹ãå¤ããã¨ãã ããå¦çã«ãã£ã¦ã¯(ãé
åã2åã¶ãåããäºã«ãªããã)array_*使ãã¨å¦ççã«ã¯éããªããããã®è¾ºã¯åæ¡ãã¦ããã£ã¦å
容ã§ã¯ããã¾ãã
ãå
ã1ä¸è¦ç´ ããé
åãã«å¯¾ãã¦
ã»å¥æ°ã ããæãåºãã¦
ã»å¤ãåã«ãã
å¦çãã¶ãã¾ããã¦ã¿ã¾ããã
å
ã«è¨æ¸¬çµæã
10åãããããæéã¨ãæå¾ã«ä½¿ç¨ã¡ã¢ãª*2ã
// arrayé¢æ°ç(ç¡åé¢æ°)
0.0016610622406006sec
0.0012879371643066sec
0.0011820793151855sec
0.0011670589447021sec
0.0011560916900635sec
0.0011970996856689sec
0.0011940002441406sec
0.0012528896331787sec
0.0010929107666016sec
0.0011200904846191sec
2097152
// arrayé¢æ°ç(åçé¢æ°)
0.002579927444458sec
0.0016429424285889sec
0.0014491081237793sec
0.0013840198516846sec
0.0014119148254395sec
0.0015451908111572sec
0.0015830993652344sec
0.0015368461608887sec
0.0014801025390625sec
0.0014641284942627sec
2097152
// foreachç(åçé¢æ°)
0.00094008445739746sec
0.00081992149353027sec
0.00080585479736328sec
0.0007941722869873sec
0.00082993507385254sec
0.00081396102905273sec
0.00085997581481934sec
0.00083804130554199sec
0.00079202651977539sec
0.00078392028808594sec
2097152
// foreachç(å é¨ã¹ãæ¸ã)
0.00034594535827637sec
0.00023603439331055sec
0.00034213066101074sec
0.00025701522827148sec
0.00025200843811035sec
0.00022697448730469sec
0.00030303001403809sec
0.00024294853210449sec
0.00026392936706543sec
0.0002589225769043sec
2097152
ããã¾ãã«ãarrayé¢æ°ä½¿ãããªãç¡åé¢æ°ã®ã»ããæ©ãã£ã½ã*3ããforeachãªãä¸ã«ãã¿ã£ã¨å¦çæ¸ãæ¹ãæ©ããã
ãã¼ã¿ã«ã¨ãã¦ã¯ãarrayé¢æ°ããforeachã®ã»ããæ©ãã(ä»åã®å¦çã®å ´å)ã
arrayé¢æ°ã®ç¡åé¢æ°ã«ã¤ãã¦ã¯ãæ³åãã¦ãªããããããªãã£ããã©ã§ãä¸å®ã®æ°ã¥ãããããçµæã§ãforeachã«ã¤ãã¦ã¯ã¾ããããã ãããªããããã(é¢æ°ã®callãã³ã¹ã0ã§ã¯ãªãããã)ã
å®è¡æéã«ã¤ãã¦ã¯ãä¸çªéããarrayé¢æ°ç(åçé¢æ°)ãã¨ä¸çªè»½ããforeachç(å
é¨ã¹ãæ¸ã)ãã§ã大ä½ã7.5åãããã®å®è¡æéã®å·®ç°ãããã®ããªãéãã£ã¡ããéããã誤差ã£ã¡ãã誤差ã ãããããã®å¾®å¦ãªæãã
ãarrayé¢æ°ç(ç¡åé¢æ°)ãã¨ãforeachç(å
é¨ã¹ãæ¸ã)ãã§ã5åå¼±ããããã
ãªã®ã§ã¾ããã¨ã©ã¤ãã¨å·¨å¤§ãªé åãæ±ããããªãæãã¯ä¸èããè¦ç´ ãããããªãããããã§ãªããã°ã誤差ã¬ãã«ãã£ã¦æ°ãããã
ããããã¨å¾ã¯ãä»ã¾ã§ã«ãã£ã¦ããæè¡èæ¯æ¬¡ç¬¬ããªã®ããªããã¨ãã
Pythonã¨ãRubyã¨ãããã¨æè¿ã®JavaScript辺ããªããã ã¨arrayé¢æ°ççãªæ¸ãæ¹ãå
ã
ãã¦ããã®ã§ããã¡ãããã®ç³»èã®äººéã¯ãå²åã¨arrayé¢æ°ççããèªã¿ãããããããããããã£ã¦è¨ã£ã¦å¥½ãå¾åãããããã«æãã¾ãã
ããã¡ããã¯å
ã
ãN88-BASICããZ80aã®ãã·ã³èªãCè¨èªçµç±ã§(ä»ã«ããã¤ããããã©)PHPã«æ¥ã¦ãã®ã§ãforeachã§ååã«ä½¿ããããã®ã§foreachã§ããããªãããããã
(C++ã®iteratorãå²ã¨è²ã
ã¨å¥½ãâ¦â¦ãã®ãautoã使ããªãã£ãé ã®æ»ã¬ãã»ã©é·ãååãå«ãã¦www)
ãã®è¾ºãåæã«ããã¨ã¯ã¾ãåèªã好ã¿ã§(ç¬
æå¾ã«ã確èªããã³ã¼ãã¯ä»¥ä¸ã®éãã§ãã
<?php // å ãã¿ä½æ $awk = range(1, 10000); // ããããå¦çã®é¢æ°ãã®1:å¥æ°å¶æ°å¤å® function a(int $i) : bool { return 1 === ($i & 1); } // ããããå¦çé¢æ°ãã®2ï¼å¤ãåã«ãã function b(int $i) : int { return $i * 2; } // arrayé¢æ°ç(ç¡åé¢æ°) function t1(array $awk) : array { return array_map(function($i) { //return $i * 2; return $i; }, array_filter($awk, function($i) { return (1 === ($i & 1)); }) ); } // arrayé¢æ°ç(åçé¢æ°) function t2(array $awk) : array { return array_map('b', array_filter($awk, 'a')); } // foreachç(åçé¢æ°) function t3(array $awk) : array { $ret = []; foreach($awk as $v) { if (true === a($v)) { $ret[] = b($v); } } return $ret; } // foreachç(å é¨ã¹ãæ¸ã) function t4(array $awk) : array { $ret = []; foreach($awk as $v) { if (1 === ($v & 1)) { $ret[] = $v * 2; } } return $ret; } // è¨æ¸¬ for($i = 0; $i < 10; ++$i) { $t = microtime(true); t4($awk); // ãããæ¸ãæãã $t_end = microtime(true); $t = $t_end - $t; echo $t , "sec\n"; } // 使ç¨ã¡ã¢ãª echo memory_get_peak_usage(true) , "\n";
*1:array_*ã ã¨ã確å®ã«ãé åã2åã¶ãåããäºã«ãªãã®ã§ãå¦ççã«ä¸å©ã«ãªã
*2:使ç¨ã¡ã¢ãªã¯å¤ãããªãã£ããããè¨æ¸¬ããªãã¦ãããã£ããªãâ¦â¦ã¨ã¯ããã¾ãããããããã£ã¦æå³ã§ã¯å¿ è¦ã ã£ãã®ãã
*3:ã³ã³ãã¤ã«çã«ã¤ã³ã©ã¤ã³å±éã¨ããã¦ãã®ããï¼