开发者

Optimize PHP function to sort registrations [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center. Closed 11 years ago.

I have a function, which look like this:

function combineFixedpriceRegistrations($regsDates, $regsMonths, $dateFrom, $dateTo) {

   // A counter
   $cnt = 0;

   // A datelooper
   $dateLoop = $dateFrom;

   // An array for the registrations
   $regs = array();

   // While it has not past the date
   while ($dateLoop <= $dateTo) {

      // Check if it is the first and the monthly registrations has not been added
      if (date('j', $dateLoop) == 1) {

         // For each monthly registration
         for ($i = 0; $i < count($regsMonths); $i++) {

            // Set the registration
            $regMonth = $regsMonths[$i];

            // Check if the date is before or equal to the date
            if (strtotime($regMonth->getDate()) <= $dateLoop) {

               // Clone the object
               $regCloned = clone $regMonth;

               // Set the date
               $regCloned->setDate(date('Y-m-d', $dateLoop));

               // Add the registration
               $regs[] = $regCloned;
            }
         }
      }

      // While there are registrations for dates
      while ($cnt<count($regsDates)) {

         // Check if the date matches
         if (date('Y-m-d', s开发者_如何转开发trtotime($regsDates[$cnt]->getDate())) == date('Y-m-d', $dateLoop)) {

            // Add the registration to the array
            $regs[] = $regsDates[$cnt];

            // Add 1 to the counter
            $cnt++;

         // Otherwise the dates does not match
         } else

            // Break out
            break;
      }

      // Add 1 to the date
      $dateLoop = strtotime('+1 day', $dateLoop);
   }
}

Can anybody figure out how to optimize it?

I will of course explain it further, if it is necessary


this is more a comment than an anwer, but it's too long for a comment. please forgive me.

it looks like you're relatively new to coding, and it's nice to see someone really wants to do things right from the first moment on: you're writing a lot of comments - like everyone should do - but most people don't.

but: 90% of your comments are completely senseless - things like...

// Clone the object
$regCloned = clone $regMonth;

// Add 1 to the counter
$cnt++;

// Break out
break;

...shouldn't need any kind of comments - those things are clear just by writing readable code.

comments shouldn't rewrite every line of code in plain english, this doesn't give any more information than the code itself. you should better use comments to explain why you're doing something, not what you're doing (in general, but of course there are obvious exceptions).


Whenever you use a for loop over the count() of an array where the only use of the loop variable is to pull the next value out of the array like this

for ($i = 0; $i < count($regsMonths); $i++) {
    $regMonth = $regsMonths[$i];
    // ... doesn't use $i again ...
}

You can simplify the code and improve its performance ever so slightly with this

foreach ($regsMonths as $regMonth) {
    // ...
}

If you need the index $i elsewhere in the loop, use

foreach ($regsMonths as $i => $regMonth) {
    // ...
}

This will improve code readability more than performance. When optimizing code, measure first, and attack the algorithm. It always helps to clearly state the purpose of the code. Comments like

// A datelooper
$dateLoop = $dateFrom;

mean little. You would be better served by documenting the intent of the method in a paragraph or two.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜