php FOR 迴圈最佳化有關問題

來源:互聯網
上載者:User
php FOR 迴圈最佳化問題
上次發個文章沒人跟,可能是問題不具體或者php基礎編程人少或者太忙.............

我想從1-33選出6個不重複的號,然後寫入資料庫
現在選號過程PHP處理的很慢還總出錯誤,誰有興趣幫我把下面的代碼最佳化下:
PHP code
";//顯示效果,本想寫入TXT文檔在傳如資料庫.                    }                }            }        }    }}?>


------解決方案--------------------
PHP code
$a  = range(1, 33);$ar = combination($a, 6);//求組合高效率的10移動法function combination($numArr,$combineLen) {  $numCt    = count($numArr);  if($combineLen > $numCt) return;  $bin    = str_pad('',$combineLen,'1');  $bin    = str_pad($bin,$numCt,'0',STR_PAD_RIGHT);      $find    = $bin;  $rs[]    = implode(' ',array_slice($numArr,0,$combineLen));  $j        = 1;  while(strrev($find) != $bin) {    $k = explode('10',$find,2);    $find = $find{0} === '0' ? strrev($k[0]).'01'.$k[1] : $k[0].'01'.$k[1];    for($i=0;$i<$numCt;$i++) $rs[$j] .=  $find[$i] ?  $numArr[$i] . " " : '';    $j++;  }  return $rs;}
------解決方案--------------------
PHP code
[User:root Time:21:21:46 Path:/home/liangdong/php]$ php permutation.php Total time used : 9738642ms[User:root Time:21:21:56 Path:/home/liangdong/php]$ cat permutation.php = $total; ++ $ndx) {                $temp[$num] = $table[$ndx];                _get_permutations($ndx + 1, $num + 1);        }}function get_permutations($n = 6) {        global $total;        global $perms;        global $fp;        if ($n <= 0 && $n > 33) {                return false;        }        $total = $n;        $fp = fopen("data.txt", "w");        _get_permutations(0, 0);        fclose($fp);        return true;}get_permutations();$end_time = microtime(true);$end_time *= 1000 * 1000;echo "Total time used : " . ($end_time - $beg_time) . "ms" . PHP_EOL;?> 
  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.