计算Cakephp中分页的执行时间

Calculate execution time of pagination in Cakephp

我正在尝试优化我对 Cakephp 中具有分页结果的页面的查询,因此我希望使用 PHP 函数 microtime() 来计算准确时间。 这样,我会更好地了解执行特定请求需要多少时间。

此外,我正在尝试通过 Cache::read 和 Cache::write 缓存分页结果,关于 Cakephp 2.x 中的缓存方法,它们都是 Cakephp 的内部函数 2.x。

因此,我现在所拥有的是: 我在想,为了准确计算整个游戏中时光倒流,我是否应该将其余代码放在 while 循环中?

如有任何帮助,我们将不胜感激。 谢谢

        $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;
        }

嗯,我发的方法确实是错误的,那可能会无限循环。最后,我发现解决这个问题的方法是将 microtime 函数放在最开始。我将一个新变量声明为 $time.

在此之后,我只是用之前声明的时间减去实际的微时间。很有魅力。

        $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");