MikoAndras.hu/en

Personal pages for Andras MIKO

Code optimizations in PHP

Code v3 – new basics

Gain with the dinamic programming was great, further refinement gives less spectacular results. In spite of that it is worth going on, because if this would be a code running in production every millisecond would be counting.

We have found with the first analyses that runtime rises exponentially. The cause for this is foreach, because this will be called exponentially more and more times with each new arriving guest. So rise the possible variations.

Attacked codepart

          $actual_variation = explode(',', $variation);
          $actual_variation[] = $rn;
          sort($actual_variation);
          $actual_variation = implode(',', $actual_variation);

An array is a complex type, so functions run on them are expensive. We use three of them in the cycle: explode, sort, implode. We should get rid of them as soon as possible.

As we store variations as string, to use the in_array() on them for comparison, we should be doing variation-calculations on this strings. This should be possible, if we create already sorted variation instead of sorting them each time.

One variation-string (for 30 people):

          $actual_variation = "1,1,1,1,2,2,3,3,4,4,4,4";

Replacing code

There are four cases how we could add room-number to a variation-list. First we go for theory, and then for the realization.

1 person room

Just throw it on the beginning of the string, because there is no smaller number.

4 person room

Just attach it to the end of the string, because there is no bigger number.

2 and 3 people room

We have to insert it after the same numbers, or if such does not exist then after the biggest number which is smaller than it.

The same in code:

            if ($rn == 1) {
              $actual_variation = $rn . "," . $variation;
            } else if ($rn == 4) {
              $actual_variation = $variation . "," . $rn;
            } else {
              for ($lrn = $rn; $lrn > 0; $lrn -= 1) {
                $pos = strrpos($variation, (string)$lrn);
                if ($pos !== false) {
                  $actual_variation = substr($variation, 0, $pos + 1) . "," . $rn .
                      substr($variation, $pos + 1, strlen($variation));
                  break;
                }
              }
            }

Resource-usage of the version:

Calculation time v3

Calculation time v3

Memory usage v3

Memory usage v3

In text:

  • Calculated in 1 second: 47 person
  • Calculated in 30 seconds: 79 person
  • 25 arriving guests: 0.0312 seconds
  • 128MB memorylimit: still very far

Rating: better and better.

All the modifications up now were created as “self-documenting” code, but there are methods for which we have to sacrifice this beauty to get any upcoming performance-boost.

Go for code obfuscation…

Pages: 1 2 3 4 5 6 7

, , ,

Leave a Reply

Your email address will not be published. Required fields are marked *

*

*

You may use these HTML tags and attributes: <a href="" title="" rel=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>