为了账号安全,请及时绑定邮箱和手机立即绑定

计算Cakephp中分页的执行时间

计算Cakephp中分页的执行时间

PHP
ITMISS 2023-04-02 10:16:56
我正在尝试优化我对 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;        }
查看完整描述

1 回答

?
神不在的星期二

TA贡献1963条经验 获得超6个赞

好吧,我发布的方法确实是错误的,可能会无限循环。最后,我发现解决这个问题的方法是将 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");

//img1.sycdn.imooc.com//6428e5ca00017b7712570594.jpg

查看完整回答
反对 回复 2023-04-02
  • 1 回答
  • 0 关注
  • 59 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信