doudengjin8251 2018-08-30 10:02
浏览 44

PHP请求在简单功能上超时

I have this function which calculates 'Book Value' of fixed assets using data from the depreciation table, Ii is working fine on localhost but when I tried to go on production, am getting 'request time out' after taking long time to load, I think I messed up with looping or something and It will be great if anyone suggest code optimization or hack to make this function faster.

I tried to limit the number of records to loop and It worked but unfortunately, I have over 20,000 data to process.

    $cat = null;
    $total_a = null;
    $assets = $this->Depreciation->get_all();

    if (!empty($assets)) {
        foreach ($assets as $asset) {

            $cost = $asset->cost + $asset->add_cost + $asset->adj_cost;
            $salve = $asset->salvage_value;
            $life = $asset->life;
            $life_t = 0;
            $counter = 0;
            $date1 = date('Y-12-31');
            $date2 = $asset->in_service;

            $diff = abs(strtotime($date2) - strtotime($date1));

            $years = floor($diff / (365 * 60 * 60 * 24));
            $months = floor(($diff - $years * 365 * 60 * 60 * 24) / (30 * 60 * 60 * 24));

            $in_service_d = date('Y-12-31');


            if ($asset->rev > 0) {
                $cost = $asset->rev;
            }


            $dep1 = ($cost - $salve) / $life * ($months / 12);
            $dep = ($cost - $salve) / $life;


            $total = 0;
            $year = 1;


            for (; $cost > $salve;) {
                if ($year == 1 && $months < 12) {
                    $life_t++;
                    $year++;
                    $cost = $cost - $dep1;
                    $counter++;
                    $total = $dep1 + $total;

                    if($in_service_d == date('Y-12-31')){
                        $total_a = $total_a + $cost;
                    }

                    $in_service_d = date('Y-12-31', strtotime("+12 months", strtotime($in_service_d)));


                } elseif ($year != 1) {
                    $life_t++;
                    if ($life_t > $life) {
                        $dep = $dep - $dep1;
                    }
                    $cost = $cost - $dep;
                    $counter++;
                    $total = $dep + $total;

                    if($in_service_d == date('Y-12-31')){
                        $total_a = $total_a + $cost;
                    }

                    $in_service_d = date('Y-12-31', strtotime("+12 months", strtotime($in_service_d)));

                }

            }

        }

    }

     return $total_a;
  • 写回答

1条回答 默认 最新

  • douli8428 2018-08-30 10:27
    关注

    Your friend is the set_time_limit() function.

    I had a similar script of exporting a formatted Excel file from a large database. The script would always run out of time whenever it exceeded over 1,000 rows.

    In the loop section of my logic, I set up the following:

    set_time_limit(30);
    

    Which would extend the allowed time to run for another 30 seconds from that point onward.

    评论

报告相同问题?

悬赏问题

  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)