-1

I am trying to optimize my query for a page with pagination results in Cakephp and therefore I am looking to calculate the exact time with the PHP function microtime(). That way, I will know better how much time it's taking to execute the certain request.

Moreover, I am trying to cache the results of the pagination through Cache::read and Cache::write, which both are internal Cakephp functions as regards caching methods in Cakephp 2.x.

Thus, what I have at this point is this: I am thinking that in order to calculate exactly the whole timelapse, should I put the rest of the code inside the while loop ?

Any help would be quite appreciating. Thanks

        $start = microtime(true);
        while ($start) {
            $this->paginate = $options;
            $resultado = $this->paginate('Doctor');  
        }
        $time_elapsed_secs = microtime(true) - $start;
        pr($time_elapsed_secs);
        foreach ($resultado AS $k => $r) {
            $hasProduct = Cache::read(__('thedoctors::') . 'hasProduct_by_dr_' . PAIS . '_' . $r['Doctor']['id'], '1day');
            if (empty($hasProduct)) {
                $hasProduct = $this->DoctorsProduct->find('all', [
                    'recursive' => -1,
                    'fields' => ['Product.*', 'DoctorsProduct.*'],
                    'joins' => [
                        ['table' => 'td_products',
                            'alias' => 'Product',
                            'type' => 'INNER',
                            'conditions' => [
                                'Product.id = DoctorsProduct.id_product'
                            ]
                        ]
                    ],
                    'conditions' => [
                        'id_doctor' => $r['Doctor']['id'],
                        'DoctorsProduct.status' => 1
                    ],
                    'order' => [
                        'Product.id ASC',
                    ]
                ]);
                Cache::write(__('thedoctors::') . 'hasProduct_by_dr_' . PAIS, $hasProduct, '1day');
            }
            $resultado[$k]['Products'] = $hasProduct;
            $resultado[$k]['Article'] = 0;
        }
Oris Sin
  • 1,023
  • 1
  • 13
  • 33
  • 2
    Why are you trying to loop here? This won't work it'll just loop infinitely cause $start is the same positive float each check. – Andy Hoffner May 19 '20 at 17:28
  • True that, I already tried it and there is a memory limit. Well, I might try a different approach to calculate the time that it takes to execute the part of the code that is coming afterwards. Thanks for your advice. – Oris Sin May 20 '20 at 07:27

1 Answers1

0

Well, the approach which I posted it is indeed wrong, that would possibly just loop infinitely. Finally, what I found as a solution to the question would be to just put the microtime function at the very beginning. I declare a new variable as $time.

After this, I am just substracting the actual microtime with the time which was declared beforewards. Works like a charm.

        $time = microtime( TRUE );
        foreach ($resultado AS $k => $r) {
            $hasProduct = Cache::read(__('thedoctors::') . 'hasProduct_by_dr_' . PAIS . '_' . $r['Doctor']['id'], '1day');
            if (empty($hasProduct)) {
                $hasProduct = $this->DoctorsProduct->find('all', [
                    'fields' => ['Product.*', 'DoctorsProduct.*'],
                    'joins' => [
                        ['table' => 'td_products',
                            'alias' => 'Product',
                            'type' => 'INNER',
                            'conditions' => [
                                'Product.id = DoctorsProduct.id_product'
                            ]
                    ],
                    ],
                    'conditions' => [
                        'id_doctor' => $r['Doctor']['id'],
                        'DoctorsProduct.status' => 1
                    ],
                    'order' => [
                        'Product.id ASC',
                    ]
                ]);
                Cache::write(__('thedoctors::') . 'hasProduct_by_dr_' . PAIS, 
            $hasProduct, '1day');
            }
            $resultado[$k]['Products'] = $hasProduct;
            $resultado[$k]['Article'] = 0;
        }
        $time = microtime( TRUE ) - $time;
        echo $time;
        die("123");

enter image description here

Oris Sin
  • 1,023
  • 1
  • 13
  • 33