简体   繁体   English

如何使该循环更快?

[英]How can I make this loop faster?

I'm using Laravel 5.2 for a project I'm working on. 我正在将Laravel 5.2用于我正在从事的项目。 Does anyone have any tips regarding how I can leverage Laravel to make this loop run more quickly? 关于我如何利用Laravel来使此循环更快地运行,是否有人有任何提示?

foreach ($purchaseYears as $purchaseYear){

$totalInventory = \App\Inventory::where('purchase_year', $purchaseYear)->count();

if ($purchaseYear < 1960) { $purchaseYear = 'Pre 1960'; }
else if ($purchaseYear >= 1960 && $purchaseYear < 1965) { $purchaseYear = '1960-64'; }
else if ($purchaseYear >= 1965 && $purchaseYear < 1970) { $purchaseYear = '1965-69'; }
else if ($purchaseYear >= 1970 && $purchaseYear < 1975) { $purchaseYear = '1970-74'; }
else if ($purchaseYear >= 1975 && $purchaseYear < 1980) { $purchaseYear = '1975-79'; }
else if ($purchaseYear >= 1980 && $purchaseYear < 1985) { $purchaseYear = '1980-84'; }
else if ($purchaseYear >= 1985 && $purchaseYear < 1990) { $purchaseYear = '1985-89'; }
else if ($purchaseYear >= 1990 && $purchaseYear < 1995) { $purchaseYear = '1990-94'; }
else if ($purchaseYear >= 1995 && $purchaseYear < 2000) { $purchaseYear = '1995-99'; }
else if ($purchaseYear >= 2000 && $purchaseYear < 2005) { $purchaseYear = '2000-04'; }
else if ($purchaseYear >= 2005 && $purchaseYear < 2010) { $purchaseYear = '2005-09'; }
else if ($purchaseYear >= 2010 && $purchaseYear < 2015) { $purchaseYear = '2010-14'; }
else if ($purchaseYear >= 2015 && $purchaseYear < 2019) { $purchaseYear = '2015-19'; }

if (isset($chartData[$purchaseYear])) {
    $prev = $chartData[$purchaseYear];
    $chartData[$purchaseYear] = $totalInventory + $prev;
    $prev = null;
} else {
    $chartData[$purchaseYear] = $totalInventory;
}

}

I think you can break the dates into a pattern, rather than just testting for each decade, something like this: 我认为您可以将日期分解为一个模式,而不仅仅是每隔十年进行测试,如下所示:

if ($purchaseYear < 1960) { 
   $purchaseYear = 'Pre 1960'; 
} else {
   $prefix = substr($purchaseYear,0,2);
   $decade = substr($purchaseYear,1,1);
   $suffix = substr($purchaseYear,-1);
   $purchYear = $prefix . $decade 
   if ($suffix<5) {
      $purchYear .= '0-' . $decade . '4'; 
   } else {
      $purchYear .= '5-' . $decade . '9'
}

Haven't tested this, but I think it will do what you'd like. 还没有测试过,但是我认为它会做您想要的。

I believe the most time consuming part should be executing the query. 我认为最耗时的部分应该是执行查询。 If you use whereIn , then I think you can execute the query only once, and then loop over the results. 如果使用whereIn ,那么我认为您只能执行一次查询,然后遍历结果。 I haven't tested it, but it should be something like this: 我没有测试过,但是应该是这样的:

$purchasesByYear = \App\Inventory::select('purchase_year',
    DB::raw('count(*) as purchase_count'))
        ->whereIn('purchase_year', $purchaseYears)
        ->groupBy('purchase_year')->get();

I think reducing the number of query executions will be the main opportunity to keep this from running slowly. 我认为减少查询执行次数将是防止其缓慢运行的主要机会。 Also, I think adding an index to purchase_year should be helpful if you have not already done so. 另外,如果您尚未将索引添加到purchase_year,我认为它会有所帮助。

Instead of a lot of elseif conditions to handle grouping the years, you could use some math. 您可以使用一些数学运算来代替对年份进行分组的其他条件。 This might actually be slightly slower, but the code would be a little less repetitive, and I believe anything you do to this part will be a micro-optimization compared to what you can do with the query part. 这实际上可能会稍慢一些,但是代码的重复性会降低一些,而且我相信与查询部分相比,您对该部分所做的任何事情都将是微优化。 My suggestion: 我的建议:

foreach ($purchasesByYear as $purchaseYear) {
    $year = $purchaseYear->purchase_year;
    if ($year < 1960) {
        $yearRange = 'Pre 1960';
    } else {
        // subtract one until the year is a multiple of five
        while ($year % 5) { $year--; }
        // then construct the range string using the starting number
        $yearRange = $year.'-'.($year+4);
    }
    if (isset($chartData[$yearRange])) {
        $chartData[$yearRange] += $purchaseYear->purchase_count;
    } else {
        $chartData[$yearRange] = $purchaseYear->purchase_count;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM