PHP- How to reduce for-loop speed

Discussion in 'PHP' started by nrkamlesh, Jun 25, 2010.

  1. #1
    <?php
    // I need the combinations for this commented numbers   
    // 1, 7, 13, 19, 25, 31, 2, 8, 14, 20, 26, 32, 3, 9, 15, 21, 27, 33, 4, 10, 16, 22, 28, 34, 5, 11, 17, 23, 29,35, 6, 12, 18, 24, 30, 36
    $string=array(1,7,13,19,25,31,2,8,14,20,26,32,3,9,15,21,27,33,4,10,16,22,28,34);
    $len=count($string);
    $c=0;
    ob_start();
    for ($e = 0; $e < $len - 6; $e++)
    {
       for ($f = $e+1; $f < $len - 5; $f++)
       {
           for ($g = $f+1; $g < $len - 4; $g++)
           {
               for ($h = $g+1; $h < $len - 3; $h++)
               { 
                   for ($i = $h+1; $i < $len - 2; $i++)
                   {
                       for ($j = $i + 1; $j < $len - 1; $j++)
                       {
                            for ($k = $j + 1; $k < $len; $k++)
                            {
                                 $c++;
                                 $output[] = $string[$e] . "," . 
                                             $string[$f] . "," . 
                                             $string[$g] . "," .  
                                             $string[$h] . "," . 
                                             $string[$i] . "," . 
                                             $string[$j] . "," . 
                                             $string[$k];
                                 ob_flush();
                            }
                            ob_flush();
                       }
                       ob_flush();
                   }
                   ob_flush();
               }
               ob_flush();
       }
       ob_flush();
    }
    ob_flush();
    }
    echo count($output);
    
    //example output
    // I need the output same like i mentioned below. Output: passed numbers $string=array(1, 7, 13, 19, 25, 31, 2, 8, 14) and the out put is below count of combinations = 36
    Array
    (
        [0] => 1,7,13,19,25,31,2
        [1] => 1,7,13,19,25,31,8
        [2] => 1,7,13,19,25,31,14
        [3] => 1,7,13,19,25,2,8
        [4] => 1,7,13,19,25,2,14
        [5] => 1,7,13,19,25,8,14
        [6] => 1,7,13,19,31,2,8
        [7] => 1,7,13,19,31,2,14
        [8] => 1,7,13,19,31,8,14
        [9] => 1,7,13,19,2,8,14
        [10] => 1,7,13,25,31,2,8
        [11] => 1,7,13,25,31,2,14
        [12] => 1,7,13,25,31,8,14
        [13] => 1,7,13,25,2,8,14
        [14] => 1,7,13,31,2,8,14
        [15] => 1,7,19,25,31,2,8
        [16] => 1,7,19,25,31,2,14
        [17] => 1,7,19,25,31,8,14
        [18] => 1,7,19,25,2,8,14
        [19] => 1,7,19,31,2,8,14
        [20] => 1,7,25,31,2,8,14
        [21] => 1,13,19,25,31,2,8
        [22] => 1,13,19,25,31,2,14
        [23] => 1,13,19,25,31,8,14
        [24] => 1,13,19,25,2,8,14
        [25] => 1,13,19,31,2,8,14
        [26] => 1,13,25,31,2,8,14
        [27] => 1,19,25,31,2,8,14
        [28] => 7,13,19,25,31,2,8
        [29] => 7,13,19,25,31,2,14
        [30] => 7,13,19,25,31,8,14
        [31] => 7,13,19,25,2,8,14
        [32] => 7,13,19,31,2,8,14
        [33] => 7,13,25,31,2,8,14
        [34] => 7,19,25,31,2,8,14
        [35] => 13,19,25,31,2,8,14
    )
    ?>
    
    PHP:

     
    Last edited: Jun 25, 2010
    nrkamlesh, Jun 25, 2010 IP
  2. rainborick

    rainborick Well-Known Member

    Messages:
    424
    Likes Received:
    33
    Best Answers:
    0
    Trophy Points:
    120
    #2
    Unless you need them for some reason in your working code, I'd certainly remove the call to ob_flush() on every iteration of your loops.

    I'm not certain if it's true for PHP, but in some languages it would be a bit faster to use a variable for the values 1 through 6 in your 'for' statements, as in:
    
    $one = 1;
    $two = 2;
    // ...
      for ($k = $j + $one; $k < $len-$two; $k++) {
    
    PHP:
     
    rainborick, Jun 25, 2010 IP
  3. Michellu

    Michellu Peon

    Messages:
    30
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    You want to reduce the speed, to be executed slower? You can call usleep(x) for every loop (it takes the number of microseconds as argument) and it will spend x microseconds doing nothing at every step.
     
    Michellu, Jun 25, 2010 IP
  4. MayurGondaliya

    MayurGondaliya Well-Known Member

    Messages:
    1,233
    Likes Received:
    38
    Best Answers:
    0
    Trophy Points:
    170
    #4
    There is another function sleep() which takes number of seconds as the argument. sleep(1) will delay the execution for 1 second and will then proceed to the statement next to the sleep(1)
     
    MayurGondaliya, Jun 25, 2010 IP
  5. w47w47

    w47w47 Peon

    Messages:
    255
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    more variables = higher server load but faster, and the other way around.

    i just don't know why you use that many FOR's :S you could use only 2 if you ask me... :S
     
    w47w47, Jun 25, 2010 IP
  6. darkdrgn2k

    darkdrgn2k Active Member

    Messages:
    159
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    53
    #6
    You have not provided enough information.

    Also i think there some confusion

    Do you wish to DECREASE the time it takes to execute the loop
    or
    Do you wish to INCREASE the time it takes to execute the loop

    To increase use SLEEP :)

    do decrease, i would generate the numbers once and store them in the file so they dont need to be generated again. They seem pritty static.

    IE:

    $output[0] ="1,7,13,19,25,31,2";
    $output[1] = "1,7,13,19,25,31,8";
    ETC

    That will reduce your loop time to 0 since you wont need to loop again :)
     
    darkdrgn2k, Jun 25, 2010 IP
    nrkamlesh likes this.
  7. nrkamlesh

    nrkamlesh Guest

    Messages:
    2
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Hi mate, i want to reduce the looping time and to generate same output. maximum numbers are 1 to 36 and it may differ depends upon the user selection for example
    array(1,10,36,11,19,14,28,27,23 .. . . . ). Mandatory condition is user must select any 7 numbers (or) above between those 36 numbers. if the user select exact 7 numbers means their will be only one combination generated. If selects 8 numbers means 8 unique combination will be generated. And i have posted the result for 9 numbers above which you can see my thread. it will generate 36 unique combinations. Now i cant able to get result for all 36 numbers because of 8.4 million arrays will be generated for all 36 numbers. please help me to generate this result quick by using PHP.
    Note: i am not gonna print this array . just i am gonna generate arrays.
     
    nrkamlesh, Jun 25, 2010 IP