计算Cakephp中分页的执行时间。

问题描述 投票:-1回答:1

我试图在Cakephp中优化我的分页结果查询,因此我想用PHP函数来计算准确的时间。microtime().这样一来,我就能更好地知道执行某个请求需要多少时间。

此外,我还想通过Cache::read和Cache::write来缓存分页的结果,就Cakephp 2.x中的缓存方法而言,这两个函数都是Cakephp内部的函数。

因此,我现在的情况是这样的。我在想,为了精确计算整个时间推移,我是否应该把其余的代码放在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;
        }
php performance cakephp cakephp-2.1 execution-time
1个回答
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");

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.