PHP循环-运行时-非常高级的微调-计算混合比例


PHP Loops - Runtime - Very advanced fine tuning - Calculate blending ratios

我想计算混合比,但我有运行时问题,在PHP中,精度为4%的7个组件似乎是极限。。。所以我试图找到一种方法来避免多余的循环,更快地计算。

我有配料和限量(这里是6)。我想找到所有的组合低于6。在第二步(此处未显示)中,我只订购了这些点击价格后。我想找最便宜的组合ingredient_x低于6。

我尝试了两种方法,只需简单地将成分相加,然后一步一步地提前中止循环(到安全循环)。

<?php
    $beginn = microtime(true);
    $loops = 0;
    $hits = 0;
    $m =  array();
    // 1. TEST
    $component[0]['ingredient_x'] = 6.95;
    $component[1]['ingredient_x'] = 65.7;
    $component[2]['ingredient_x'] = '';
    $component[3]['ingredient_x'] = 2;
    $component[4]['ingredient_x'] = '';
    $component[5]['ingredient_x'] = '';
    $component[6]['ingredient_x'] = '';
    /*
    Results:
        With abort Loops
            Loops: 285.188
            Hits: 285.077
            Seconds: 1,707 sec.
        Without:
            Loops: 736.281
            Hits: 285.077
            Seconds: 6,582 sec.
    */
    // 2. TEST
    $component[0]['ingredient_x'] = 6.95;
    $component[1]['ingredient_x'] = 6.7;
    $component[2]['ingredient_x'] = '';
    $component[3]['ingredient_x'] = 2;
    $component[4]['ingredient_x'] = '';
    $component[5]['ingredient_x'] = '';
    $component[6]['ingredient_x'] = '';
    /*
    Results:
        With abort Loops
            Loops: 735.244
            Hits: 735.167
            Seconds: 4,467 sec.
        Without:
            Loops: 736.281
            Hits: 735.167
            Seconds: 3,191 sec.
    */

    $abort_loops = 1;

    for ($m[0] = 0; $m[0] <= 100;                                                   $m[0] += 4) {
    for ($m[1] = 0; $m[1] <= (100 - $m[0]);                                         $m[1] += 4) {
    for ($m[2] = 0; $m[2] <= (100 - $m[0] - $m[1]);                                 $m[2] += 4) {
    for ($m[3] = 0; $m[3] <= (100 - $m[0] - $m[1] - $m[2]);                         $m[3] += 4) {
    for ($m[4] = 0; $m[4] <= (100 - $m[0] - $m[1] - $m[2] - $m[3]);                 $m[4] += 4) {
    for ($m[5] = 0; $m[5] <= (100 - $m[0] - $m[1] - $m[2] - $m[3] - $m[4]);         $m[5] += 4) {
                    $m[6]  = (100 - $m[0] - $m[1] - $m[2] - $m[3] - $m[4] - $m[5]);
        $loops++;
        if ($abort_loops) {
            $r = 0;
            $do_break = 0;
            // Checking ingredient_x sum, component by component
            for ($i = 0; $i <= 6; $i++) {
                $r += ($m[$i] * ($component[$i]['ingredient_x'] / 100));
                // If Limit is reached, end all following loops
                if ($r > 6) {
                    $m[$i] = 100;
                    // Cant break here, because of inner loop ...
                    $do_break = 1;
                }
            }
            // ... so do it outside
            if ($do_break) {
                break;
            }
            $hits++;
        } else {
            // just sum ingredient_x
            $r = ($m[0] * ($component[0]['ingredient_x'] / 100)) +
                      ($m[1] * ($component[1]['ingredient_x'] / 100)) +
                      ($m[2] * ($component[2]['ingredient_x'] / 100)) +
                      ($m[3] * ($component[3]['ingredient_x'] / 100)) +
                      ($m[4] * ($component[4]['ingredient_x'] / 100)) +
                      ($m[5] * ($component[5]['ingredient_x'] / 100)) +
                      ($m[6] * ($component[6]['ingredient_x'] / 100));
            if ($r <= 6) {
                $hits++;
            }
        }
    }
    }
    }
    }
    }
    }
    print('Loops: ' . number_format($loops, 0, '', '.') . '<br>');
    print('Hits: ' . number_format($hits, 0, '', '.') .  '<br>');
    print('Seconds: ' . number_format((microtime(true) - $beginn), 3, ',', '') . ' sec.');

?>

但这在很大程度上取决于原料。正如您在测试1中看到的那样(中止循环快5倍)和测试2(简单求和方式更快)。

有可能使这些循环更快吗?更好的"中止循环"代码?有没有更快的方法找到最便宜的组合?

一种方法是使用简单的数学来改进总结:

// just sum ingredient_x
$r = (
          $m[0] * $component[0]['ingredient_x'] +
          $m[1] * $component[1]['ingredient_x'] +
          $m[2] * $component[2]['ingredient_x'] +
          $m[3] * $component[3]['ingredient_x'] +
          $m[4] * $component[4]['ingredient_x'] +
          $m[5] * $component[5]['ingredient_x'] +
          $m[6] * $component[6]['ingredient_x']
    );
;
if ($r <= 600) {
    $hits++;
}

如果你把你的极限提高到你极限的100倍,那么所有除以100的因子都可以完全去掉。

a0 / 100 + a1 / 100 + a2/100 + ... a6/100

与相同

(a0 + a1 + a2 + ... + a6) /100

这只需要一个师,而不是7个师。

如果你把你的限制调整到100*你的限制,即使是这种划分也是可以避免的。

我的机器的速度提高了大约10-15%。