小妹剛剛改投PHP門下。領導叫我把這段代碼的執行效率優化一下
我現在知道的優化就是小循環外面,好像在這沒啥用。
請問各位大俠我該怎麼優化ne ? 領導說放內存裡什麼的。
基本就是2個大數組不停的循環算權重。
<?php error_reporting(E_ALL & ~E_NOTICE & ~E_DEPRECATED & ~E_STRICT); class weight { private $_aItems = array(); private $_aTable = array(); private $_aDict = array(); private $_aMatchs = array(); private $_aShow = array(); function __construct() { } public function newItems($aItems){ //添加新的檢索內容 if (!is_array($aItems)) $aItems = (array)$aItems; $this->_aItems = $aItems; $this->_aMatchs = array(); $this->_aShow = array(); } public function newTable($aTable){ if (!is_array($aTable)) $aTable = (array)$aTable; $this->_aTable = $aTable; $this->generateDict(); } private function generateDict() { //將字典處理成數組形式 $convert = function($value) { $value = str_replace('|', ',', $value); $value = explode(',', $value); return $value; }; $this->_aDict = array_map($convert, $this->_aTable); } public function getMatchs() { //返回對照表 return $this->_aMatchs; } public function getShow($sRule = 'debug') { /*返回格式化的結果集 * $sFormat: 指定輸出格式 */ if (empty($this->_aItems)||empty($this->_aTable)) //字典源文件不存在 return false; if (empty($this->_aShow)) { /*匹配表還沒有生成,自動調用相應的命令生成*/ $this->loopTable(); } $makeDumpStr = function($value, $key) use (&$dumpStr) { //生成導出文件的文本 if (count($value) >1) { foreach ($value as $valueOne) { $valueStr .= $valueOne. ','; } $dumpStr .= $this->_aItems[$key] . "\t匹配多個記錄號\t". $valueStr ."\r\n"; } else { $dumpStr .= $this->_aItems[$key] . "\t匹配惟一記錄號\t". $value[0] ."\r\n"; } }; switch($sRule) { case 'debug': print_r($this->_aShow); break; case 'json': return json_encode($this->_aShow); break; case 'txt': $timeExport = date("Y/M/D h:i:s"); $dumpStr = ''; $rFile = fopen('dump.txt', 'w'); array_walk($this->_aShow, $makeDumpStr); $sContent = <<<EOT ========DUMP-FILE-{$timeExport}========================= {$dumpStr} EOT; fwrite($rFile, $sContent); fclose($rFile); break; default: return $this->_aShow; break; } } private function loopTable() { //遍歷 foreach ($this->_aItems as $iItemKey=> $sItemLine) { $this->matchElement($iItemKey); //print_r($this->_aMatchs); $this->match2Show($iItemKey); //print_r($this->_aShow); //echo "-----------------"; } //print_r($this->_aMatchs); //print_r($this->_aShow); } private function matchElement($iKey) { $iMax = 0; foreach ($this->_aDict as $iDictKey => $aDictLine) { foreach($aDictLine as $sDictElement) { $str = $this->_aItems[$iKey]; if(strstr($str, $sDictElement)){ //匹配到一個元素,計數器+1 ++$this->_aMatchs[$iKey]['keyring'][$iDictKey]; } } if (!$this->_aMatchs[$iKey]['keyring'][$iDictKey]) { //沒有匹配到內容 $this->_aMatchs[$iKey]['keyring'][$iDictKey] = 0; } if ($iMax< $this->_aMatchs[$iKey]['keyring'][$iDictKey]) $iMax = $this->_aMatchs[$iKey]['keyring'][$iDictKey]; $this->_aMatchs[$iKey]['index'] = array( 'key' => $iDictKey, 'count' => $iMax ); } } private function match2Show($iKey) { //將對照表轉化為結果集 $multiMatch = array(); //echo "ikey =". $iKey.", "; foreach ($this->_aMatchs[$iKey]['keyring'] as $iMatchKey => $iVal) { if ($iVal< $this->_aMatchs[$iKey]['index']['count']) { //這個值比最大值小 //echo "x"; continue; } else { //這個值跟最大值相等,將結果累加到記錄中 //echo "y"; $multiMatch[] = $iMatchKey; } } if (count($multiMatch)> 1) //多於一條記錄匹配值相同 $this->_aShow[$iKey] = $multiMatch; else //匹配值最大值唯一 $this->_aShow[$iKey] = array($this->_aMatchs[$iKey]['index']['key']); } } $aItems = array( 'chinaisbig', 'whichisnot', ..... 上萬條 ..... 'totalyrightforme', ); $aTable = array( 'china,is|small', 'china,big|me', ..... 上千條 ..... 'china,is|big,wich|not,me', ); $weight = new weight(); $weight->newItems($aItems); $weight->newTable($aTable); $weight->getShow('debug'); ?>