Problems related to phpFOR loop optimization

Source: Internet
Author: User
The problem of loop optimization in phpFOR was not followed by a post last time. it may be that the problem is not specific, or the php basic programming staff is few or too busy ............. I want to select 6 non-repeated numbers from 1-33, and then write the numbers into the database. Currently, PHP is very slow to process and there are still errors. who is interested in helping me optimize the following code: PHPcode & lt ;? Php FOR loop optimization
No one followed the Last Post. it may be because the problem is not specific or the php programming is very small or busy .............

I want to select 6 distinct numbers from 1-33 and write them to the database.
Currently, PHP is still handling very slowly errors during the code selection process. who is interested in helping me optimize the following code:
PHP code
  
  "; // Display effect. I want to write a TXT file to the file, for example, database. }}}}?>


------ Solution --------------------
PHP code
$ A = range (1, 33); $ ar = combination ($ a, 6); // Evaluate the efficient 10-moving 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 ;}
------ Solution --------------------
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;?>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.