Как увеличить скорость выполнения кода.Я использовал некоторые вычисления в PHP - PullRequest
0 голосов
/ 21 мая 2018

Как я могу сделать этот расчет быстрее, очень медленно.Я много раз пробовал по-другому, но не могу.Пожалуйста, кто-нибудь может помочь мне в этом.

Я использовал Ajax и Jquery, чтобы получить значение из пользовательского ввода и вернуть это значение пользователю в Ajax.i использовал cakephp2.5.некоторые значения получены из базы данных и моего кода контроллера.поэтому, пожалуйста, помогите сделать этот код быстрее.

public function getData(array $query=null){
    $findquery=$this->find('all',$query);
    return $findquery;
}

public function gettotal()
{
    $this->layout = 'ajax';
    $pcbx = $_POST['pcbx'];
    $pcby = $_POST['pcby'];
    $pcb_qty = $_POST['pcb_qty'];
    $smt_pads = $_POST['smt_pads'];
    $hole_pads = $_POST['hole_pads'];
    $fine_pitch = $_POST['fine_pitch'];
    $layers = $_POST['layers'];
    $pcbper = $_POST['pcbper'];
    if (!($pcbx > 350) && !($pcbx < 50) && !($pcby > 350) && !($pcby < 50) && !($pcb_qty < 1)) {
        $sum = ($pcb_qty * $pcby * $pcbx) / 1000000;
        if (!$layers == '0') {
            $query = array(
                'conditions' => array('layer' => $layers),
                'fields' => array('layer_value')
            );
            $check = $this->Layer_setup_pricemaster->getData($query);
            $layer_price = $check[0]['Layer_setup_pricemaster']['layer_value'];
        }

        /* get SMT Pads Point price based on condition the sum of pcbx, pcby and pcb Quantity is greater than or equal to min price and less than or equal to max_price from Per_point_pricemaster table*/
        $check101 = $this->Per_point_pricemaster->getData(array('condition' => array('id' => 22)));
        $end_price = $check101[21]['Per_point_pricemaster']['min_price'];
        if($smt_pads != 0 && $smt_pads != ''){
            if($sum >= $end_price){
                $query2 = array(
                    'conditions' => array('min_price ' => $end_price),
                    'limit' => 1,
                    'fields' => array('price')
                );
            }else{
                $query2 = array(
                    'conditions' => array('min_price <=' => $sum, 'max_price >=' => $sum),
                    'limit' => 1,
                    'fields' => array('price')
                );
            }
            $check2 = $this->Per_point_pricemaster->getData($query2);
            $point_price = $check2[0]['Per_point_pricemaster']['price'];
        }else {
            $point_price = 0;
        }

        /* get Through Hole Pads Point price based on condition the sum of pcbx, pcby and pcb Quantity is greater than or equal to min price and less than or equal to max_price from through_hole_padsmaster table*/
        $check102 = $this->Through_hole_padsmaster->getData(array('condition' => array('id' => 22)));
        $end_price2 = $check102[21]['Through_hole_padsmaster']['min_price'];
        if($hole_pads != 0 && $hole_pads != ''){
            if ($sum >= $end_price2) {
                $query3 = array(
                    'conditions' => array('min_price ' => $end_price2),
                    'limit' => 1,
                    'fields' => array('price')
                );
            }else {
                $query3 = array(
                    'conditions' => array('min_price <=' => $sum, 'max_price >=' => $sum),
                    'limit' => 1,
                    'fields' => array('price')
                );
            }
            $check3 = $this->Through_hole_padsmaster->getData($query3);
            $hole_pads_price = $check3[0]['Through_hole_padsmaster']['price'];
        }else {
            $hole_pads_price = 0;
        }

        /* Get the Stencil Price from stencil_pricemaster table which is by default id=5 */
        $query4 = array(
            'conditions' => array('id' => 5),
            'fields' => array('including_tax')
        );
        $check4 = $this->Stencil_pricemaster->getData($query4);
        $stencil_price = $check4[0]['Stencil_pricemaster']['including_tax'];

        if($fine_pitch != 0 && $fine_pitch != ''){
            /* Get BGA/QFN/Fine Pitch Point Price from bga_qfnmaster table wich is by default id=1 */
            $query5 = array(
                'conditions' => array('id' => 1),
                'fields' => array('price')
            );
            $check5 = $this->Bga_qfnmaster->getData($query5);
            $bga_qfn_price = $check5[0]['Bga_qfnmaster']['price'];
        }else {
            $bga_qfn_price = 0;
        }

        if(!empty($pcbper) && $pcbper != '0'){
            $pointsum1 = $smt_pads * $point_price * $pcb_qty * $pcbper;
            $pointsum2 = $hole_pads * $hole_pads_price * $pcb_qty * $pcbper;
            $pointsum3 = $fine_pitch * $bga_qfn_price * $pcb_qty * $pcbper;
            if (!$layers == '0') {
                $total = $pointsum1+ $pointsum2 + $pointsum3 + $layer_price + $stencil_price;
            } else {
                $total = $pointsum1 + $pointsum2 + $pointsum3;
            }
        } else{
            $pointsum1 = $smt_pads * $point_price * $pcb_qty;
            $pointsum2 = $hole_pads * $hole_pads_price * $pcb_qty;
            $pointsum3 = $fine_pitch * $bga_qfn_price * $pcb_qty;
            if (!$layers == '0') {
                $total = $pointsum1 + $pointsum2 + $pointsum3 + $layer_price + $stencil_price;
            } else {
                $total = $pointsum1 + $pointsum2 + $pointsum3;
            }
        }
        echo $total = number_format($total);
    } else {
        echo "Calculating...";
    }
    exit;
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...