Smarty template nesting include and fetch performance test _php tutorial

Source: Internet
Author: User
Tags smarty template
Method One, use the {include file= "CHILD.TPL"} in the parent template to include the child template directly

Advantages:

1, is advantageous to the module division and the template reuse.

2. When the nesting level is not many, the structure of the template is clear and the contents and structure of the template are known in the past.

3, only need a smarty instance can do all the things, reduce the use of system resources.

Insufficient:

1. Variables in child templates may conflict with the parent template's variables

2, multiple nesting time, the probability of variable name conflict increases, for all variables assigned to the difficulty also increased.

3, sub-template can not be manipulated, for example, can not be set $cache_id, $compile _id, come to multiple results.

4, poor extensibility. If you need to add a new template, you need to modify the template, and you need to modify the program.

Method Two, by $smarty->fetch () the child template data as a variable assign to the parent template

Advantages:

1, also conducive to the division of modules, template reuse

2, the structure of the template is equally clear

3, sub-template as a separate object, controllability strong

4. Good extensibility

5, each child template is a separate object, there is no problem of variable name conflict

Insufficient:

1, if the sub-template more time, need to use multiple Smarty instances to implement, PHP code appears bloated

2, the use of more system resources

3 、......

At that time, out of the performance requirements, and in the absence of testing, take for granted that the performance of the include is higher than the fetch performance, all I use the Include this method, the result as the system functions continue to increase, feel more and more complex to write, This is a big part of the problem with design, but the nesting of templates is one of the problems, so go back and consider using Fetch. Test the performance of this method before making a decision, testing the situation of a single template and multiple templates, respectively

Single Child template test:

There are three page test1.php,test2.php,test3.php. All three of these pages output the same content. test1.php templates are not nested, test2.php templates use a include,test3.php to implement nested nesting of child templates using fetch. Average time (in MS) required to access individual pages using Apache's own AB test

The first test, using Ab-n 10000 for the above three pages 10 times, each cumulative 10,000 visits, the results are as follows.

1 2 3 4 5 6 7 8 9 test1.php
test2.php test3.php test2/test1 test3/test1
5.178 5.442 6.097 1.050984936 1.177481653
5.253 5.972 6.027 1.136874167 1.147344375
5.223 6.039 5.987 1.156232051 1.146276087
5.533 5.997 6.02 1.083860474 1.08801735
5.557 6.308 6.03 1.135144862 1.085117869
5.248 6.002 5.998 1.14367378 1.142911585
5.211 5.933 6.003 1.138553061 1.151986183
5.303 6.031 6.048 1.137280784 1.140486517
5.213 5.923 6.033 1.136197967 1.15729906
Total 47.719 53.647 54.243 1.124227247 1.136717031

The second test, using Ab-n 100000 to the above three pages 1 times, each cumulative 100,000 visits, the results are as follows.

test1.php
test2.php test3.php test2/ Test1 test3/test1
1 5.723 7.874 8.55 1.375851826 1.493971693

The first test data, test1 and test2 fluctuations more obvious, test3 is relatively stable, this phenomenon makes me feel very strange. Judging from the average data, the test results are expected, test1 performance is the best, test2 than test1 more than 12.42% of the time, test3 than test1 more than 13.67% of the time, but Test2 and test3 the gap is not large, Only about 1.3% of the difference.

The result of the second Test, three pages of the execution time than the 1:1.37:1.49, it is strange that the fluctuations of the greater ...

Multiple sub-template tests:

There are also three pages of test1.php,test2.php,test3.php. All three of these pages output the same content. test1.php templates are not nested, test2.php templates use 10 include to embed 10 sub-templates, test3.php embed 10 sub-templates using 10 fetch. Average time (in MS) required to access individual pages using Apache's own AB test

Because of the time, I only had one Test ab-n 50000, the data is as follows

Ab-n 50000 test1.php test2.php test3.php Test2/test1 Test3/test1
1 5.68 6.054 6.028 1.06584507 1.061267606

Although the testing methods and tools may not be very good, but through the comparison of these data, in the actual application in the end to adopt which method, I think we should be in the bottom of the heart.

Shortcomings and mistakes also hope that we can discuss more exchanges.

http://www.bkjia.com/PHPjc/322601.html www.bkjia.com true http://www.bkjia.com/PHPjc/322601.html techarticle method One, using the {include file= "CHILD.TPL"} in the parent template directly includes the following advantages: 1, facilitates the division of modules and the reuse of templates. 2. When the nesting level is not many ...

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.