MikoAndras.hu/en

Personal pages for Andras MIKO

Code optimizations in PHP

Code v5 – strings a little bit unconventinal

With the first XDebug profile we can see that string-manipulation functions run too many times. This is no miracle, as we use them to create the always actual variations what we compare with our sped-up solution. It would be good to hack on them again.

2 x substr() + strlen()

2 x substr() + strlen()

Attacked code

              $pos = strrpos($variation, (string)$lrn);
              if ($pos !== false) {
                $actual_variation = substr($variation, 0, $pos + 1) . "," . $rn .
                    substr($variation, $pos + 1, strlen($variation));
                $inserted = true;
                break;
              }

What we do here is the insertion of the 2 and 3 bedroom numbers so we do not have to bother with sorting, but we can create already sorted variations. What we cannot get rid of is the definition of the position, because we have to know where to put the new number. What we can change however is the way we insert it.

Right now we use the schoolyard solution, in which we cut the parts before and after the position and then concatenate the three parts together.

With a – not especially for this written – built-in function parametrized the correct way we can get an “insert a new bit into the string at a position” function. This is the substr_replace().

Originally it was intended to replace parts in strings, but it will accept 0 as the length of the part-to-be-replaced, so it takes 0 character from the position and replaces it with another part.

Replacement code

              $pos = strrpos($variation, (string)$lrn);
              if ($pos !== false) {
                $actual_variation = substr_replace($variation, "," . $rn, $pos + 1, 0);
                break;
              }

In this place we can only talk about fractions-of-seconds, but for the sake of completeness, the resource-usage of the version:

Calculation time v5

Calculation time v5

Memory usage v5

Memory usage v5

You can only see the memory usage graph for the v4, because the usage is identical for the two versions, the only difference is in the runtime (and that only above 28-30 person)

KCachegrind profile after modifications:

substr_replace()

substr_replace()

(To run the profiler I have called the code through a browser, for 50 guests)

In text:

  • Calculation in 1 second: 97 person
  • 30 másodpercen belül kiszámítható: ? persons (stops because of memory limit)
  • 25 arriving guests: 0.005 seconds
  • 128MB memorylimit: at calculating 121 guests

Rating: expert work.

We were concentrating on speed, sacrificing the memory. Our code became overly hungry for being fast. The good news is, we can have it do a diet without losing any of the speed…

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>