【问题标题】:Calculate execution time of pagination in Cakephp在 Cakephp 中计算分页的执行时间
【发布时间】:2020-05-19 16:34:21
【问题描述】:

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

此外,我正在尝试通过 Cache::read 和 Cache::write 来缓存分页的结果,它们都是 Cakephp 内部的函数,关于 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;
        }

【问题讨论】:

  • 你为什么要在这里循环?这行不通,它只会无限循环,因为 $start 每次检查都是相同的正浮点数。
  • 没错,我已经试过了,但有内存限制。好吧,我可能会尝试一种不同的方法来计算执行之后的部分代码所需的时间。谢谢你的建议。

标签: php performance cakephp cakephp-2.1 execution-time


【解决方案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");

【讨论】:

    猜你喜欢
    • 2010-10-26
    • 1970-01-01
    • 1970-01-01
    • 2021-06-21
    • 2020-07-22
    • 1970-01-01
    • 1970-01-01
    • 2018-01-28
    • 2017-05-10
    相关资源
    最近更新 更多