ããã°ã©ã ãæ¸ãã¦ããã¨ãå ¥åå¤ãè¾æ¸ã«å«ã¾ãã¦ãããã調ã¹ãããããªãã¨ãããã¾ãããã¨ãã°ãã¦ã¼ã¶ã¼ã«é½éåºçåãå ¥åããã¦ããããæ£ããé½éåºçåã§ãããã©ããã調ã¹ãããã¨ãããããªãã¨ãããããããã¾ããã
ãã®ãããªå 容ãPHPã§æ¸ãéããã¼ã«é½éåºçåãæã¤ãããªé£æ³é åãä½ãç¿æ £ããã人ã¯å¤ãã¯ãã§ããããã¯å ¸åçãªé£æ³é åã®ä½¿ãæ¹ã¨ãããã§ãããã
<?php $prefs = array( "åæµ·é" => true, "é森" => true, // ... "æ²ç¸" => true, ); if (isset($prefs[$input])) { // é½éåºçåãæ£ããæã®å¦ç }
ä¸æ¹ã§ãin_array
é¢æ°ã使ãããæ¹ãèãããã¾ãã
<?php $prefs = array( "åæµ·é", "é森", // ... "æ²ç¸", ); if (in_array($input, $prefs)) { // é½éåºçåãæ£ããæã®å¦ç }
çªç¶ã§ãããã¯ã¤ãºã§ãããã®2ã¤ã®ãã¡ãã©ã¡ãã®æ¹ãè¯ãã³ã¼ãã§ããããï¼
é£æ³é åããç¹å®ã®ãã¼ãæã¤è¦ç´ ã«ã¢ã¯ã»ã¹ããã³ã¹ãã¯O(1)ã§ããã®ã«å¯¾ããé åããç¹å®ã®è¦ç´ ãæ¢ãã³ã¹ãã¯O(N)ã§ããããä¸è¬è«ã¨ãã¦ã¯é£æ³é åã使ãæ¹ã好ã¾ããã¨è¨ããã§ããã*1ãããããè¨ç®éã®è°è«ã¯Nã大ããå ´åã«ã¯æ£ããã§ãããNãå°ããç¯å²ã§ã¯ä¿æ°ãå®æ°é ãªã©ä»ã®è¦ç´ ã®å½±é¿ãç¡è¦ã§ããªãã¯ãã§ãã常ã«åè ã®æ¹ãè¯ãã³ã¼ãã§ããã¨è¨ããã ãã®ææãæã ã¯æã£ã¦ããã®ã§ããããã
ããã§ãä»åPHP 5.6.30ããã³PHP 7.1.5ãç¨ãã¦ãé åãµã¤ãºã¨ã¢ã¯ã»ã¹åæ°ãå¤ããªãã両è ã®é度ã測å®ãã¦ã¿ã¾ãããæ¬ç¨¿ã§ã¯ãã®çµæãç´¹ä»ãã¾ãã
PHP 5ç³»ã§ã¯é£æ³é åã¢ã¯ã»ã¹ã®æ¹ãéã
ä»åãé
åã¨é£æ³é
åã¨ã§ãè¾æ¸ãã¼ã¿ã®æ§ç¯æéã¨æ¤ç´¢æéã®åãæ¯è¼ãã¦ã¿ã¾ãããã¨ããã®ããä¸å®è¦æ¨¡ã®é
åã«ãªãã¨æ§ç¯ã«ãããæéãç¡è¦ã§ããªãããããã¨ãæ¤ç´¢ãé
ãã¦ãæ§ç¯ãéãæ¹ããã¼ã¿ã«ã§ã¯æå©ã«ãªãå¯è½æ§ãããããã§ãã
æ©éã§ãããPHP 5.6.30ã§ã®å®é¨çµæã®ã°ã©ãã示ãã¾ãã両対æ°ã°ã©ããªã®ã§è¦æ¹ã«æ³¨æãã¦ãã ããã
é åãµã¤ãºã¨ããã®ã¯è¾æ¸ã«å«ã¾ããåèªæ°ã§ããã¾ããã¢ã¯ã»ã¹åæ°ã¨ããã®ã¯ãã®è¾æ¸ã«å¯¾ããæ¤ç´¢ã®åæ°ã§ããä¸è¨ã°ã©ãã§ã¯ã¢ã¯ã»ã¹åæ°ãåºå®ãã¦é åãµã¤ãºãåã ã§å¢ããã¦ãããã¨ãããã¨ããã¾ããã
ãããè¦ãã¨ãã¢ã¯ã»ã¹åæ°ã8åããç¶æ³ã§ããã°é£æ³é åã®æ¹ã1.2ãã1.5åç¨åº¦ã¯éãã¨è¨ãããã§ããã¾ããã¢ã¯ã»ã¹åæ°128åã«ãªãã¨å·®ã絶æçã«éãã¾ããæ¤ç´¢åæ°ãå¤ãå ´åã¯ï¼å½ç¶ã§ããï¼æ¤ç´¢ã®è¨ç®éã®å·®ãæ¯é çã§ãããã¨ããããã¾ãã
é£æ³é åã®æ¹ããã¼ã¿ã®æ§ç¯ã«æéããããããã¢ã¯ã»ã¹åæ°2åã§ã¯äºè§ç¨åº¦ã«ãªãã¾ãããäºè§ã§ããã°é£æ³é åã使ã£ãæ¹ãäºæ ã¯å°ãªããã§ãã
PHP 7ç³»ã§ã¯é åã¢ã¯ã»ã¹ã«é転ããããã¨ããã
é¢ç½ããã¨ã«ãPHP 7ç³»ã ã¨ç¶æ³ãå¤ãã£ã¦ãã¾ããPHP 7.1.5, ã¢ã¯ã»ã¹åæ°8åã®ã°ã©ããè¦ã¦ã¿ã¾ãããã
é åãµã¤ãºãå°ãããã¡ã¯é£æ³é åæå©ã ã£ãã®ããé åãµã¤ãº2048ãããã§é åãé転ãããµã¤ãº65536ãããã§é£æ³é åãåé転ãããã¨ãããããªçµæã«ãªã£ã¦ãã¾ããä½ãèµ·ãã£ãã®ã§ããããã
é
åãµã¤ãºãå°ãããã¡ã¯é
åã¨é£æ³é
åã¨ã§æ§ç¯ã³ã¹ãã«å¤§ããªå·®ã¯ããã¾ããããã®æ®µéã§å·®ã«ãªã£ã¦ããã®ã¯ãin_array($key, $array)
ãé¢æ°å¼ã³åºãã§ããã®ã«å¯¾ããisset($array[$key])
ã¯opcodeã«ã³ã³ãã¤ã«ããã¦é¢æ°å¼ã³åºãã«ãªããªãã¨ãããã¨ã§ããPHPã®é¢æ°å¼ã³åºãã¯ä»ã®opcodeå®è¡ã«æ¯ã¹ãã¨ã³ã¹ããé«ãã®ã§ããã®å·®ã§é£æ³é
åã®æ¹ãéããªã£ã¦ããã®ã§ãã
é åãµã¤ãºãä¸ç¨åº¦ã«ãªã£ã¦ããã¨ãé åã¨é£æ³é åã®æ§ç¯ã³ã¹ãã®å·®ãæ¯é çã«ãªã£ã¦ãã¾ããPHP 7ããã¯çã®é åã¨è¨ãã¹ããã¼ã¿æ§é ãå°å ¥ããã¦ãã*2ãé£æ³é åã¨æ¯ã¹ã¦ç°¡æ½ãªãã¼ã¿æ§é ã§æ¸ãããã«ãªãã¾ããããã®ãããåããµã¤ãºã§ããã°é£æ³é åããé åã®æ¹ãæ§ç¯ã³ã¹ããä½ããPHP 5ã®ã¨ãã¯è¦ãããªãã£ãé転ç¾è±¡ãèµ·ããã¨ããããã§ãã
ããã«é åãµã¤ãºã大ãããªã£ã¦ããã¨O(N)ã¨O(1)ã¨ããã¢ã¯ã»ã¹æã®è¨ç®éã®å·®ãå¹ãã¦ããã®ã§ããµããã³é£æ³é åã®æ¹ãæå©ã«ãªãã¾ãã
ãã®ããã«ãã¢ã¯ã»ã¹åæ°8ä»è¿ã§ã¯è¤éãªç¶æ³ãè¦ãããããã§ããä¸æ¹ãã¢ã¯ã»ã¹åæ°32ã§ããã°åºæ¬çã«ã¯é£æ³é åã®æ¹ãæå©ã¨ãªãã¾ãã
ã¾ããã¢ã¯ã»ã¹åæ°8192åã§ã¯çµ¶æçãªå·®ãã¤ãã¦ãã¾ãã
ãã®ããã«ãã¢ã¯ã»ã¹åæ°ã大ãããªã£ã¦ããã¨äºæ³éãé£æ³é åã®æ¹ãæå©ã ã¨ãããã¨ããããã¾ãã
ä¸æ¹ãã¢ã¯ã»ã¹åæ°ãå°ãªãå ´åã«éãã°é åãæ¡ç¨ããå¯è½æ§ãã¼ãã§ã¯ãªãã¨è¨ãããã§ããã¨ã¯ãããããã°ã©ã ã®æ¹ä¿®ãªã©ã§ã¢ã¯ã»ã¹åæ°ãå¢ããå¯è½æ§ãèããã¨åºæ¬çã«ã¯é£æ³é åãæ¡ç¨ãã¦ããæ¹ãç¡é£ã§ãããã
ãã®æç« ãã©ãèªããã«ã¤ãã¦ã®è£è¶³ï¼2017/5/25追è¨ï¼
ä¸è¬è«ã¨ãã¦ãæ®æ®µã®ã³ã¼ãã£ã³ã°ã«ããã¦ãã¤ã¯ããã³ãã®çµæãåèã«ããããã®ã¯è¯ãç¿æ £ã§ã¯ããã¾ããããã¨ãã°ãechoã¨printfã¨ã§ã©ã¡ããéããã¿ãããªå 容ã«å¼ã£å¼µããã¦æ¸ãã³ã¼ããå¤ããå¿ è¦ã¯ãªãã§ãããããããããããã«ããã¯ã«ãªã£ã¦ãããã¨ãå¤æããããå¤æããå¾ã§å¤ããã°è¯ãã¯ãã§ãã
ãã®ä¸æ¹ã§ããã¤ã¯ããã³ãã®é度差ããªãçã¾ããã®ãããã®åå ã調ã¹ããã¨ã§ä½ããã®æ°ããç¥è¦ãå¾ããã¨ãããã¯ãã§ããæ¬ç¨¿ã§ä¸çªãä¼ããããã£ãã®ã¯ããã®é¢ç½ãã«ã¤ãã¦ã§ããã
å ãã¦ãä»åã¯è¨ç®éã®è©±é¡ããªã¼ãã¼ã®éãã®æããã¨ãã£ãé¨åãçãè¾¼ãã ããã§ããããã®ãããã¯äººã«ãã£ã¦æè¦ãç°ãªãã®ããããã¾ãããç§å人ã®æè¦ã¨ãã¦ã¯è¨ç®éã®è¦³ç¹ã§ä¸å©ãªå®è£ ãæ¡ç¨ããéã¯å¸¸ã«çç±ãå¿ è¦ã ã¨æã£ã¦ãã*3ãæ¬ç¨¿ããã®ãããªåæã§æ¸ããã¦ãã¾ãããç°ãªãæè¦ããã£ã¦ãããããã¯ç¡ãã¨æãã¾ãã
ã¾ã¨ã
PHPã®é£æ³é
åã¨é
åã®in_array()
ã¨ã§ãã©ã¡ããæå©ããæ¤è¨ãã¦ã¿ã¾ããã
- PHP 5.6ã§ã¯é£æ³é åãæå©
- PHP 7ã§ã¯ãçã®é
åãã®ãããã§é
åãæå©ãªç¶æ³ããããã
- ã¢ã¯ã»ã¹åæ°å°ãé åãµã¤ãºä¸ç¨åº¦ã®å ´åã«éã
- é åã®æ¹ãæ¤ç´¢ã³ã¹ãã¯é«ãããæ§ç¯ã³ã¹ããä½ããã
- ç¾å®ã«ã¯é
åãæ£è§£ã¨ããç¶æ³ã¯å°ãªãã¯ã
- ã¢ã¯ã»ã¹åæ°ã¨é åãµã¤ãºã®ä¸¡æ¹ã極端ã«å°ãªãå ´åã¯é åã®æ¹ãèªã¿ããããã
é£æ³é åã®æ¹ãæå©ã ã¨ããã®ã¯ç¹ã«é©ãã®ãªãçµè«ã§ãããPHP 7ã®ãçã®é åãã«ããæ§è½åä¸ãå£éè¦ããã®ã¯é¢ç½ãã¨æãã¾ããã
ãã¤ãã£ãã³ã¼ãã«ã³ã³ãã¤ã«ãããããªè¨èªã§ããã°ãè¾æ¸ãµã¤ãºãå°ããã¨ãã¯é£æ³é
åãããé
åãæ£è§£ã¨ããç¶æ³ãããããã¨æããã§ãããPHPã®å ´åã¯in_array()
ãé¢æ°å¼ã³åºãã§ãããã¨ã®ããã«ãã£ã大ããããã®ãããªç¶æ³ã¯ç¢ºèªã§ãã¾ããã§ããã
追試ç¨è³æ
çè ã®æå ã®ãã·ã³ã¯Core Mæ¡ç¨ã®ãã¼ããã·ã³ã§ãããTurbo Boostã®ããã§çµæãå®å®ããªãã£ããããã®ã§ãçµæã®ä¿¡ç¨æ§ã¯ä¿è¨¼ã§ãã¾ããã追試ãããã¨æãããæ¹ã¯ä¸è¨URLãã確èªãã ããã
*1:ãã¨ãã°å ¥åå¤ãè¾æ¸ã«åå¨ããªããã¨ã示ãå ´åã«ãé åã ã¨å ¨è¦ç´ ã¨Nåã®æ¯è¼ãçºçãã¾ãããé£æ³é åã ã¨ããã·ã¥å¤ãä¸è´ããè¦ç´ ï¼å¹³åã§ã¯1è¦ç´ ï¼ã¨æ¯è¼ããã°è¯ãã®ã§ãè¨ç®ã®å¹çãæ ¼æ®µã«éããã¨ã«ãªãã¾ã
*2:åèè³æï¼ãPHP7で変わること ——言語仕様とエンジンの改善ポイントã
*3:éã«echoã¨printfã¯ãªã¼ãã¼ãåããªã®ã§ã©ã¡ãã§ãè¯ãã¨æã£ã¦ãã¾ã