我具有此功能,该功能使用折旧表中的数据计算固定资产的“账面价值”,II在localhost上运行良好,但是当我尝试进行生产时,花了很长时间才能获得“请求超时”负载,我想我弄乱了循环之类的东西,如果有人建议通过代码优化或修改以使此功能更快,那将是很棒的。
我试图限制要循环的记录的数量,但确实可行,但不幸的是,我要处理的数据超过20,000。
$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;
答案 0 :(得分:0)
您的朋友是set_time_limit()
函数。
我有一个类似的脚本,可以从大型数据库中导出格式化的Excel文件。每当脚本超过1000行时,它总是会耗尽时间。
在我的逻辑的loop
部分,我设置了以下内容:
set_time_limit(30);
从该点开始,将允许的时间再延长30秒。