PHP 大數組迴圈問題_PHP教程

來源:互聯網
上載者:User
小妹剛剛改投PHP門下。領導叫我把這段代碼的執行效率最佳化一下

我現在知道的最佳化就是小迴圈外面,好像在這沒啥用。

請問各位大俠我該怎麼最佳化ne ? 領導說放記憶體裡什麼的。

基本就是2個大數組不停的迴圈算權重。

_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 = <<_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');?>

http://www.bkjia.com/PHPjc/625800.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/625800.htmlTechArticle小妹剛剛改投PHP門下。領導叫我把這段代碼的執行效率最佳化一下 我現在知道的最佳化就是小迴圈外面,好像在這沒啥用。 請問各位大俠我該...

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.