📜 ⬆️ ⬇️

PHP: template performance

What am I talking about?


Oh yeah, I wanted to tell you about a little test that I spent at leisure. The fact is that I love reinventing bicycles (don't kick me for this innocent hobby). Therefore, when I was a poachy programmer, I thought about the implementation of the template engine (and whoever did not think, let him throw a stone at me).
If you want to compare the performance of famous template engines - sorry, another time
The topic of this article is to study the performance of some particular cases of using simple templates.

Actually what was analyzed


I think many people know what templates are, who do not know, read Wikipedia , templates are a way of representing a view (View)
Actually there are three approaches to this business (not counting third-party developments and modules like XML / XSLT):
  1. Connecting ordinary PHP files where the template is described
    Reading, parsing and executing a template in some sample language
    Point 2 but with caching of the template code compiled in PHP

    Testing sources


    Simple connection


     function file_included () {
         $ vars ['some'] = 'some';
         include 'included.php';
     }
    

    Simple require


     function file_required () {
         $ vars ['some'] = 'some';
         require 'included.php';
     }
    

    Reading and executing a file


     function file_evaled () {
         $ vars ['some'] = 'some';
         $ content = file_get_contents ('included.php');
         eval ('?>'. $ content);
     }
    

    Parsing regexp


     function file_tpl_preg () {
         $ vars ['some'] = 'some';
         $ content = file_get_contents ('included.tpl');
         $ content = preg_replace ('# \ {(\ w +) \} #', '<? = $ vars [\' \ 1 \ ']?>', $ content);
         eval ('?>'. $ content);
     }
    

    Parsing string replacements


     function file_tpl_strtr () {
         $ vars ['some'] = 'some';
         $ content = file_get_contents ('included.tpl');
         $ content = strtr ($ content, array (
             '{' => '<?  $ vars [\ '',
             '}' => '\']?> ',
         ))
         eval ('?>'. $ content);
     }
    

    Regexp with caching time change check


     function file_tpl_preg_cashe () {
         $ file = 'included';
         if (filemtime ($ file. '. tpl.php') <filemtime ($ file. '. tpl')) {
             $ content = file_get_contents ('included.tpl');
             $ content = preg_replace ('# \ {(\ w +) \} #', '<? = $ vars [\' \ 1 \ ']?>', $ content);
             file_put_contents ($ file. '. tpl.php', $ content);
         }
         $ vars ['some'] = 'some';
         include $ file. '. tpl.php';
     }
    

    Regexp with caching simple existence check


     function file_tpl_manual_cashe () {
         $ file = 'included';
         if (! file_exists ($ file. '. tpl1.php')) {
             $ content = file_get_contents ('included.tpl');
             $ content = preg_replace ('# \ {(\ w +) \} #', '<? = $ vars [\' \ 1 \ ']?>', $ content);
             file_put_contents ($ file. '. tpl1.php', $ content);
         }
         $ vars ['some'] = 'some';
         include $ file. '. tpl1.php';
     }
    

    results


    Functions were simply called in a loop, file system access caching is disabled.
    Type of test
    Time
    % of speedy
    file_included0.00021430100.00%
    file_required0.00021500100.33%
    file_evaled0.00022488104.94%
    file_tpl_preg0.00024208112.96%
    file_tpl_strtr0.00022802106.40%
    file_tpl_preg_cashe0.00024305113.42%
    file_tpl_manual_cashe0.00023100107.79%


    I will explain the results:
    1. file_included - simple PHP template incl, fastest
      file_required - the same connection, only in profile
      file_evaled - separately read and execute PHP template, a bit slower
      file_tpl_preg - parsing the regexp template, 10-12 percent slower than the inclus
      file_tpl_strtr - parsing with quick replacements in lines, an inflexible approach, but only 5-6 percent slower
      file_tpl_preg_cashe - parsing regexpom and caching with automatic check for cache aging. The speed is almost the same as the same without caching, it looks like filemtime is a very slow function
      file_tpl_manual_cashe - caching only with checking the existence of the cache (for parsing, you need to delete the old cache). Pretty good results, only 6-7 percent slower than the inclusion.
      ')

      Draw your own conclusions :)

Source: https://habr.com/ru/post/27620/


All Articles