简体   繁体   中英

How can I make this loop faster?

I'm using Laravel 5.2 for a project I'm working on. Does anyone have any tips regarding how I can leverage Laravel to make this loop run more quickly?

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. 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.

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;
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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