计算Cakephp中分页的执行时间

我正在尝试优化我对 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;

        }


ITMISS
浏览 63回答 1
1回答

神不在的星期二

好吧,我发布的方法确实是错误的,可能会无限循环。最后,我发现解决这个问题的方法是将 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");
打开App,查看更多内容
随时随地看视频慕课网APP