Some other tips for PHP developers

    Inspired by this .

    I decided to recall some of the performance features of PHP.

    I note that I included in my short list only what usually causes surprise among junior developers, with whom I had to work.
    I think everyone knows about trivial things, like “single quotes instead of double quotes, so I’ll try to surprise someone.

    The results and conclusions are made on the basis of several versions of PHP that run on servers familiar to me, namely 5.2.6 from Debian Lenny, 5.3.2 from Ubuntu, and 5.2.14 from dotdeb. Perhaps on other platforms, there are differences.

    • file_get_contents
      It's no secret that file_get_contents uses ( memory mapping ), the gain from which is especially noticeable on large files.
      The consequence of this: it
      simplexml_load_string( file_get_contents ('file.xml') )
      works faster than:
      simplexml_load_file('file.xml')
      It seems that similar simplexml_load_file functions are based on ordinary fopen / fread, which leads to a difference in speed.

      NB: DOM-> loadFile also accelerates pretty well, and some other functions with similar behavior.

      If you parse a file with \ n delimiters (or for example CSV, TSV, or something similar), I advise you to replace file () with
      explode(PHP_EOL, file_get_contents('file.xml'));
      Thank you for the help with PHP_EOL LoneCat
      The growth will be even greater than in the case of xml.
      Apparently, file () is not very well implemented.
    • count () and sizeof ()
      UPD: sizeof () is a synonym for count (), it works faster, thanks merkushin for the correction.
    • Notices, etc.
      Notice, it's terrible, yes.
      But if your junior developer does not want to acknowledge their importance, tell him that it takes time for PHP to generate one notice, for which you can bypass and increment an array of about 30 elements.
    • foreach
      The foreach loop, in almost every PHP performance article, is anathema.
      In practice, not everything is so scary. Creepy designs, like:
      while (list($key, $value) = each($item))
      in real conditions, are often slower.
      If you skip $ key, then this design loses foreach about 30-40%.
    • JSON vs XML
      I can only say that by switching to json files for configuration, I won 20-30% on kernel initialization. JSON is pleasing to the eye, hierarchical and fast.
      Also, json_decode works faster without a second argument (generating an object, not an array), but not significantly.
    • mb_ereg vs preg_match
      POSIX - expressions are slow, this, again, is well known.
      But the Oniguruma engine, which is used in the mb_ereg function, and its mb counterparts, does not agree with this, and in about two thirds of cases, it overtakes the much-praised preg_match.
    • IGBinary for serialization.
      This is a very fast expansion , with a very compact output.
    • file_exists and include
      Checking file_exists () then making include is cheaper than checking the return of include () if, for example, you are not sure if the file is in place.
    • Include again
      I don’t know why, but include_once often loses in design speed with forced verification (we write all the included files to the array).
    • Static vars
      A static class variable, the best place to quickly retrieve global data, an increase of 5 to 10 times is noticed.
    • Finally, a couple of tips
      Show yourself the runtime in milliseconds, not fractions of a second.
      It motivates a lot (compare, “100 milliseconds” and “0.1 seconds”), and is easier to read.
      It is very important to collect information, not only about the absolute speed, but also to analyze the contribution of individual subsystems: the kernel, data interface, rendering, etc.
      I configured my profiler (on a testing machine) to throw exceptions when there was an anomalous slowdown in any code sections (example: “Achtung! 30% of the time to connect to MySQL”).


    All conclusions were obtained as a result of personal observations and tests.
    It is possible that on your system the results will be different.

    I note that a lot depends on your architecture, and almost any advice should be checked on your code, not trusting completely someone else's experience.

    Therefore, I deliberately did not indicate the absolute numbers that you can find yourself, for example, at http://phpbench.com/ (not mine)

    Good luck and quick code!

    Also popular now: